3rd Unit PDF
3rd Unit PDF
The C String
is stored as an array of characters. The difference between a character array and a C string is that the
string in C is terminated with a unique character ‘\0’.
Example:
char string_name[size];
In the above syntax string_name is any name given to the string variable and size is used to define the
length of the string, i.e the number of characters strings will store.
There is an extra terminating character which is the Null character (‘\0’) used to indicate the termination
of a string that differs strings from normal character arrays.
C String Initialization
A string in C can be initialized in different ways. We will explain this with the help of an example. Below
are the examples to declare a string with the name str and initialize it with “ C Programming”.
Syntax:
Syntax:
Syntax:
Syntax:
Program on strings:
// C program to illustrate strings
#include <stdio.h>
#include <string.h>
int main()
printf("%s\n", str);
int length = 0;
length = strlen(str);
return 0;
Output:
C Programming
Length of string str is 12
Different functions used for reading and writing strings with examples:
The user can input strings to the program via the console. There are three methods to read strings:
Using scanf();
Using getchar();
Using gets();
Example:
Char place[10];
Scanf(“%s”,place);
Using getchar(); : getchar() function is used o tead successive single characters from the input and
place them into a character array.thus an entire line of text can be read and stored in an array.
Char ch;
Using gets( ) function : gets() function is available in the <stdio.h> header file this is a simple
function with one string parameter and called as follows:
Gets(strings);
For example:
Char name[20];
Gets(name);
Printf(“%s”,name);
Using printf();
Using putchar();
Using puts();
Using printf() functions: the printf function with %s format to print strings to the screen the format %s
can be used to display an array of characters that is terminated by the null character.
For example:
Printf(“%s”,name);
Program on printf()
Using putchar( ) function: like getchar( ), c supports another character handling function
putchar() to output the values of character variables it takes the following
syntax:
Char ch =A;
Using puts() function: The puts() function works exactly opposite to gets() function it outputs a
string to the scteen from memory that is from the array we declare until reaches the null character.
Gets(name);
Puts(name);
strcat() Function
The strcat() function in C is used for string concatenation. It will append a copy of the source string to
the end of the destination string.
The terminating character at the end of dest is replaced by the first character of src.
Parameters
Return value
Example
// C Program to illustrate the strcat function
#include <stdio.h>
int main()
strcat(dest, src);
return 0;
Output:
2. strlen() Function
The strlen() function calculates the length of a given string. It doesn’t count the null character ‘\0’.
Parameters
Return Value
Example
// C program to demonstrate the strlen() function
#include <stdio.h>
#include <string.h>
int main()
return 0;
Output
String: GeeksforGeeks
Length: 13
3. strcmp() Function
The strcmp() is a built-in library function in C. This function takes two strings as arguments and compares
these two strings lexicographically.
Parameters
Example
#include <stdio.h>
#include <string.h>
int main()
// 'str2'
// 'str3'
// 'str1'
return 0;
Output
strncmp()
This function lexicographically compares the first n characters from the two null-terminated strings and
returns an integer based on the outcome.
Syntax
4. strcpy
The strcpy() is a standard library function in C and is used to copy one string to another. In C, it is present
in <string.h> header file.
Syntax
char* strcpy(char* dest, const char* src);
Parameters
Return Value
Example
#include <stdio.h>
#include <string.h>
int main()
// defining strings
char dest[20];
strcpy(dest, source);
// printing result
return 0;
Output
Source: GeeksforGeeks
Destination: GeeksforGeeks
strncpy()
The function strncpy() is similar to strcpy() function, except that at most n bytes of src are copied.
If there is no NULL character among the first n character of src, the string placed in dest will not be
NULL-terminated. If the length of src is less than n, strncpy() writes an additional NULL character to dest
to ensure that a total of n characters are written.
Syntax
5. strchr()
The strchr() function in C is a predefined function used for string handling. This function is used to find
the first occurrence of a character in a string.
Syntax
Parameters
Here, str is the string and ch is the character to be located. It is passed as its int promotion, but it is
internally converted back to char.
Return Value
Example
#include <stdio.h>
#include <string.h>
int main()
if (result != NULL) {
else {
"string\n",
ch);
return 0;
Output
strrchr() Function
In C, strrchr() function is similar to strchr() function. This function is used to find the last occurrence of a
character in a string.
Syntax