strings
strings
Introduction to strings
In C language, programmers can also create array of character data type known
as string. Also, a string is one- dimensional array of characters terminated by a
NULL character.
In this lesson we will study each and every detail about string handling in great detail.
We can declare a variable of string data type much like declaring an array of any other data
type.
Initializing string
We can also initialize the string when it is declared. There are a number of methods
using which we can initialize a string.
2
For example,
The compiler automatically adds the NULL character at the end of the string. So
the above string will be saved in memory as:
String input
There are a number of methods to accept input of string at run-time. Every method
is different from the other and have certain advantages and disadvantages
associated with them.
#include<conio.h>
#include<stdio.h>
void main()
// string declaration
char string[15];
3
scanf("%s", &string);
Output:
The answer is not what we expected. There is a problem when you accept input of a
string using scanf (). Once the user enters the space, the string after that is not accepted.
So only the string entered before the space is accepted as the final string.
#include<conio.h>
#include<stdio.h>
void main()
// string declaration
char string[15];
gets ( string);
}
4
Output:
C provides a set of library functions for handling strings. C string handling refers to a set
of functions to apply various operations on strings in the C standard library. These
functions include operations like copying, concatenation and searching.
Following are some of the standard library functions for manipulating strings.
Library functions
✓ strlen ()
✓ strcat ()
✓ strcpy ()
✓ strcmp ()
✓ strrev ()
1. strlen ()
This function is used return the length of string. The length does not include the NULL
character. For many of the string operations, knowing the length of the string is vital.
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
5
{
char str[15];
int index, j;
scanf("%s",&str);
// apply strlen ()
{
printf(“\nThe string is not a palindrome”);
goto end;
//label
end:
getch();
2. strcat ()
Syntax,
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
string :- ");
scanf("%s",&str1);
scanf("%s",&str2);
// apply strcat ()
getch();
3. strcpy ()
This function is used to copy the content of one string into another string. The content of
the second string before copying is overwritten by the new content.
7
Syntax,
To implement strcpy ()
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
scanf("%s",&str1);
scanf("%s",&str2);
// apply strcpy ()
p rintf ("\nContent of second string after copying the content of first in it :- %s", str2);
getch();
4. strcmp ()
This function is used to compare two strings. If two strings are absolutely same the function
returns 0, otherwise it returns the difference in the ASCII codes of the two.
Syntax,
8
To implement strcmp ()
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20], str2[20];
int difference ;
scanf("%s",&str1);
printf ("Enter second string :- ");
scanf("%s",&str2);
// apply strcmp ()
if ( difference == 0)
else
getch();
}
9
Exercise 2
List all string library functions that are available in C++ and other programming
languages that are not listed in this lesson. Give brief description about all of them and
also explain the syntax with the help of an example.
5. strrev ()
Syntax,
To implement strrev()
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
char str[20];
scanf("%s",&str);
//apply strrev ()
strrev (str);
10
Output: