1
Introduction to strings
Traditionally a string is considered to be a sequence of characters, either in form of literal
constants or as some kind of variables. Variable strings are subject to change and the
literal constants are fixed in length and do not change. A string is a derived data type that
stores a sequence of elements, typically characters.
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.
Figure 10.1 Declaration of a string.
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,
Figure 10.2 Initializing string using double quotes
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.
Let us try some programs to accept string from the user
To accept string input from the user using scanf ()
#include<conio.h>
#include<stdio.h>
void main()
// string declaration
char string[15];
3
//input string from the user
printf("Enter any string :- ");
scanf("%s", &string);
//display the string
printf("String entered by you :- %s", string);
getch();
Output:
Enter any string :- abhi gupta
String entered by you :- abhi
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.
To accept string input from the user using gets ()
#include<conio.h>
#include<stdio.h>
void main()
// string declaration
char string[15];
//input string from the user
printf("Enter any string :- ");
gets ( string);
//display the string
printf("String entered by you :- %s", string);
getch();
}
4
Output:
Enter any string :- abhi gupta
String entered by you :- abhi gupta
String library functions
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.
To check if the string is palindrome or not
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
5
{
char str[15];
int index, j;
//accept string input
printf ("Enter any string :- ");
scanf("%s",&str);
// apply strlen ()
index = strlen(str) -1;
for ( j = 0; j <= index/2; j++)
{
if ( str [j] != str [index – j] )
{
printf(“\nThe string is not a palindrome”);
goto end;
printf(“\nThe string is a palindrome”);
//label
end:
getch();
2. strcat ()
This function is used to concatenate to strings. In simpler terms, concatenation means to
append a string at the end of another string.
Syntax,
char * strcat ( char *ch1, const char *ch2 );
6
To implement concatenation function
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
char str1[20], str2[10];
//input two strings
printf ("Enter first
string :- ");
scanf("%s",&str1);
printf ("Enter second string :- ");
scanf("%s",&str2);
// apply strcat ()
strcat (str1, str2);
p rintf ("\nFirst string after applying concatenation :- %s", str1);
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,
char * strcpy ( char * ch1, const char * ch2 );
To implement strcpy ()
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
char str1[20], str2[20];
//input two strings
printf ("Enter first string :- ");
scanf("%s",&str1);
printf ("Enter second string :- ");
scanf("%s",&str2);
// apply strcpy ()
strcpy( str2, str1);
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
int strcmp ( const char * str1, const char * str2 );
To implement strcmp ()
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20], str2[20];
int difference ;
//input two strings
printf ("Enter first string :- ");
scanf("%s",&str1);
printf ("Enter second string :- ");
scanf("%s",&str2);
// apply strcmp ()
difference = strcmp(str1, str2);
if ( difference == 0)
p rintf ("\nStrings are same");
else
printf ("\nStrings are not same");
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 ()
This function is used to reverse the elements of the string.
Syntax,
char * strrev ( char * str1 );
To implement strrev()
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
char str[20];
//input the string
printf ("Enter any string :- ");
scanf("%s",&str);
//apply strrev ()
strrev (str);
10
// display the reversed string
printf ("\nThe string after strrev () :- %s", str);
getch();
Output:
Enter any string :- abhinav
The string after strrev () :- vanihba