C: Functions We've talked a little bit about functions in the past - they're pieces of code that can be executed on command. In fact we've been using functions right since the very start of this process, we just haven't really talked about them in depth - this is what this tutorial is all about. C String class Examples and Tutorial. C may use the C char variable type and string functions but they rely on a null termination and proper memory allocation to hold the string. The ANSI C GNU string classes included in the C standard library attempt to simplify string manipulation by automating much of the memory allocation and management.

Apr 12, 2012  C Tutorial 5 - Strings, Getline, Concatenation, and String Functions. We will also learn how to concatenate strings together as well as use some predefined string functions from the string. C Character String Functions Tutorial - C treats a string as a null-terminated array of characters. The string functions require the header file string.h to provide their prototype and the character functions use ctype.h as their header file. String Functions. Use SAQL string functions to format your measure and dimension fields. Endswith Returns true if the string ends with the specified characters.

< cpp
Standard Library Headers
Freestanding and hosted implementations
Named requirements
Language support library
Concepts library(C++20)
Diagnostics library
Utilities library
Strings library
Containers library
Iterators library
Ranges library(C++20)
Algorithms library
Numerics library
Input/output library
Localizations library
Regular expressions library(C++11)
Atomic operations library(C++11)
Thread support library(C++11)
Filesystem library(C++17)
Technical Specifications
Strings library
Null-terminated strings
Byte strings
Multibyte strings
Wide strings

The C++ strings library includes support for three general types of strings:

  • std::basic_string - a templated class designed to manipulate strings of any character type.
  • std::basic_string_view(C++17) - a lightweight non-owning read-only view into a subsequence of a string.
  • Null-terminated strings - arrays of characters terminated by a special null character.


  • 4Additional support


The templated class std::basic_string generalizes how sequences of characters are manipulated and stored. String creation, manipulation, and destruction are all handled by a convenient set of class methods and related functions.

Several specializations of std::basic_string are provided for commonly-used types:

Type Definition
std::u8string(since C++20)std::basic_string<char8_t>
std::u16string(since C++11)std::basic_string<char16_t>
std::u32string(since C++11)std::basic_string<char32_t>


The templated class std::basic_string_view provides a lightweight object that offers read-only access to a string or a part of a string using an interface similar to the interface of std::basic_string.

Several specializations of std::basic_string_view are provided for commonly-used types:

Defined in header <string>
Type Definition
std::string_view(since C++17)std::basic_string_view<char>
std::wstring_view(since C++17)std::basic_string_view<wchar_t>
std::u8string_view(since C++20)std::basic_string_view<char8_t>
std::u16string_view(since C++17)std::basic_string_view<char16_t>
std::u32string_view(since C++17)std::basic_string_view<char32_t>
(since C++17)

[edit] Null-terminated strings

Null-terminated strings are arrays of characters that are terminated by a special null character. C++ provides functions to create, inspect, and modify null-terminated strings.

There are three types of null-terminated strings:

[edit] Additional support


The string library also provides class template std::char_traits that defines types and functions for std::basic_stringand std::basic_string_view(since C++17). The following specializations are defined:

template<>class char_traits<char>;
template<>class char_traits<char8_t>;
(since C++20)
(since C++11)
template<>class char_traits<char32_t>;
(since C++11)

[edit]Conversions and classification

The localizations library provides support for string conversions (e.g. std::wstring_convert or std::toupper) as well as functions that classify characters (e.g. std::isspace or std::isdigit).

[edit]See also

C documentation for Strings library
Retrieved from 'https://en.cppreference.com/mwiki/index.php?title=cpp/string&oldid=111299'

String is an array of characters. In this guide, we learn how to declare strings, how to work with strings in C programming and how to use the pre-defined string handling functions.

We will see how to compare two strings, concatenate strings, copy one string to another & perform various string manipulation operations. We can perform such operations using the pre-defined functions of “string.h” header file. In order to use these string functions you must include string.h file in your C program.

String Declaration

Method 1:

Method 2: The above string can also be defined as

In the above declaration NULL character (0) will automatically be inserted at the end of the string.

What is NULL Char “0”?
'0' represents the end of the string. It is also referred as String terminator & Null Character.

String I/O in C programming

Read & write Strings in C using Printf() and Scanf() functions


Note: %s format specifier is used for strings input/output

Read & Write Strings in C using gets() and puts() functions

C – String functions

C String function – strlen


size_t represents unsigned short
It returns the length of the string without including end character (terminating char ‘0’).

Example of strlen:


strlen vs sizeof
strlen returns you the length of the string stored in array, however sizeof returns the total allocated size assigned to the array. So if I consider the above example again then the following statements would return the below values.

strlen(str1) returned value 13.
sizeof(str1) would return value 20 as the array size is 20 (see the first statement in main function).

C String function – strnlen


size_t represents unsigned short
It returns length of the string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value.

Example of strnlen:

Length of string str1 when maxlen is 30: 13
Length of string str1 when maxlen is 10: 10

Have you noticed the output of second printf statement, even though the string length was 13 it returned only 10 because the maxlen was 10.

C String function – strcmp

It compares the two strings and returns an integer value. If both the strings are same (equal) then this function would return 0 otherwise it may return a negative or positive value based on the comparison.

See the URLs at the label for more related links.Suicideboys (stylized as $uicideboy$) is an American hip hop duo from New Orleans, Louisiana, founded in 2014 by cousins Ruby da Cherry and $crim (pronounced Scrim). Auto tune ep uicideboy. Hip-hop duo originating in New Orleans, Louisiana, United States.Owners of, producing most of their songs there.

If string1 < string2 OR string1 is a substring of string2 then it would result in a negative value. If string1 > string2 then it would return positive value.
If string1 string2 then you would get 0(zero) when you use this function for compare strings.

Example of strcmp:


C String function – strncmp

size_t is for unassigned short
It compares both the string till n characters or in other words it compares first n characters of both the strings.

Example of strncmp:


C String function – strcat

It concatenates two strings and returns the concatenated string.

Example of strcat:



C String function – strncat

It concatenates n characters of str2 to string str1. A terminator char (‘0’) will always be appended at the end of the concatenated string.

Example of strncat:


C String function – strcpy

It copies the string str2 into string str1, including the end character (terminator char ‘0’).

Example of strcpy:


C String function – strncpy

char *strncpy( char *str1, char *str2, size_t n)
size_t is unassigned short and n is a number.
Case1: If length of str2 > n then it just copies first n characters of str2 into str1.
Case2: If length of str2 < n then it copies all the characters of str2 into str1 and appends several terminator chars(‘0’) to accumulate the length of str1 to make it n.

Example of strncpy:


C String function – strchr

It searches string str for character ch (you may be wondering that in above definition I have given data type of ch as int, don’t worry I didn’t make any mistake it should be int only. The thing is when we give any character while using strchr then it internally gets converted into integer for better searching.

Example of strchr:


C String function – Strrchr

It is similar to the function strchr, the only difference is that it searches the string in reverse order, now you would have understood why we have extra r in strrchr, yes you guessed it correct, it is for reverse only.

Now let’s take the same above example:


Why output is different than strchr? It is because it started searching from the end of the string and found the first ‘f’ in function instead of ‘of’.

C String function – strstr

It is similar to strchr, except that it searches for string srch_term instead of a single char.

Example of strstr:


String Functions In Dev C N Dev C++ Videos

You can also use this function in place of strchr as you are allowed to give single char also in place of search_term string.