String functions: strlen, strcmp, strrev, strcpy, strlwr, strupr
String handling functions
Definition of String:
String is the set of characters,digits and symbols. We can also define string as the array of characters. The end of the string is marked with a special character , the ‘\0’ that means Null character. The size in a character string represents the maximum number of characters that the string can hold.
POINTS TO REMEMBER
A string is an array of characters. It contains characters, symbols, numbers etc.
Syntax:
char string_name[string_size];
As in above syntax, char refers to the character data type that will be stored in the array, string_name is the name of the string variable and string_size indicates the number of characters that can be stored inside the array.
Example
char name[10];
char address[10];
This declares the name as a character array (string) variable that can hold a maximum of 10 characters.
String is simply a sequence of characters, so to declare a string, we have to put data type char with string name with square brackets.
Initialization of Strings
Let us take a string:
char name[10]={‘c’, ‘ ’, ‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘\0’};
or second method
char name[10]= “c program” ;
Each character is treated as an element of the array name and is stored in the memory as follows:
name[0] | name[1] | name[2] | name[3] | name[4] | name[5] | name[6] | name[7] | name[8] | name[9] |
‘c’ | ‘ ’ | ‘p’ | ‘r’ | ‘o’ | ‘g’ | ‘r’ | ‘a’ | ‘m’ | ‘\0’ |
Index 0 | Index 1 | Index 2 | Index 3 | Index 4 | Index 5 | Index 6 | Index 7 | Index 8 | Index 9 |
When the compiler sees a character string, it terminates it with an additional null character means empty. Thus, the element name[9] holds the null character ‘\0’ at the end. When declaring a character array, we must always allow one extra element space for the null terminator.
Program 42 Write a program to input character wise in array and display them.
#include <stdio.h>
int main()
{
char name[10]={'c','','p','r','o','g','r','a','m','\0'};
printf("The given string is : %s",name);
return 0;
}
Program 43 Write a program to input string in array and display them.
#include <stdio.h>
int main()
{
char name[10]="c program";
printf("The given string is : %s",name);
return 0;
}
Array of String
A two dimensional array of characters is called an array of strings. It is also known as a table of strings. It is very useful for solving string sorting problems. In other words, an array of strings can be declared and handled like a two dimensional array.
Syntax:
char string_name[MAX][SIZE]:
As in above syntax, char refers to character data type, string_name is the name of the string variable and in two dimensional array, [MAX] indicates the number of strings and [SIZE] indicates the maximum number characters associated with string.
Example
char name[5][10];
In this example, here is a two dimensional character array. The array is initialized with five character strings with maximum size ten.
Program to demonstrate table of strings
#include <stdio.h>
int main()
{
int i;
char name[5][30]={"Dipendra","Manisha","Suyog","Ram","Laxman"};
for(i=0;i<5;i++)
{
printf("%s\n",name[i]);
}
return 0;
}
String Handling Functions
The C library supports a large number of string handling functions that can be used to carry out many of the string manipulation. The header file #include<string.h> is used for string manipulation functions. Some of the common string manipulation functions are:
strlen() function
The strlen() function returns the length of a string which takes the string name as an argument.
Syntax:
l=strlen(str);
where, str is the name of string and l is the length of the string, returned by function.
Program 44 Write a program to find the length of a string.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int l;
printf("Enter any string = ");
scanf("%s",str);
l=strlen(str);
printf("Length of string is %d ", l);
return 0;
}
strrev() function
The strrev() function is used to reverse the given string.
Syntax:
strrev(str);
where str is string to be reversed.
Program 45 Write a program to reverse the given string.
#include <stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("Enter any string = ");
scanf("%s",str);
strrev(str);
printf("Reverse of string is %s ",str);
return 0;
}
strcpy() function
The strcpy() function is used to copy one string into another string.
Syntax:
strcpy(destination,source) or
strcpy(strl,str2);
where strl,str2 are two strings.
The content of string str2 is copied on string strl.
Program 46 Write a program to copy one string into another string.
#include <stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
printf("Enter any string = ");
scanf("%s",str1);
strcpy(str2,str1);
printf("After copying the string is %s ", str2);
return 0;
}
strcat() function
The strcat() function is used to join or concatenate one string into another string.
Syntax:
strcat(strl,str2);
where str1,str2 are two strings. The content of string str2 is concatenated with string str1.
Program 47 Write a program to concatenate two strings into one string.
#include <stdio.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
printf("Enter two strings = ");
scanf("%s%s",str1,str2);
strcat(str1,str2);
printf("After copying the string is %s ", str1);
return 0;
}
strcmp( ) function
The strcmp() function is used to compare two strings, character by character and stops comparison when there is a difference in the ASCII value or the end of any one string and returns ASCII difference of the character that is integer.
Syntax:
v=strcmp(strl,str2);
Where, str1 and str2 are two strings to be compared and v is a value returned by the function which is either zero(equal), positive value(descending) or negative value(ascending).
Program 48 Write a program to compare two strings
#include <stdio.h>
#include<string.h>
int main()
{
char str1[50],str2[50];
printf("Enter two strings : ");
scanf("%s%s",str1,str2);
int v=strcmp(str1,str2);
if(v==0)
printf("Both strings are same and have same ASCII value");
else if(v>0)
printf("%s comes after %s or %s has more ASCII value than %s",str1,str2,str1,str2);
else
printf("%s comes before %s or %s has more ASCII value than %s",str2,str1,str2,str1);
return 0;
}
strlwr() function
The strlwr() function is used to convert upper case characters of string into lower case characters.
Syntax:
strlwr (str);
where str is string to be converted into lower case characters.
Program 49 Write a program to convert the given string into lower case.
#include <stdio.h>
#include<string.h>
int main()
{
char str[50];
printf("Enter a string : ");
scanf("%s",str);
strlwr(str);
printf("Given string in lower case is %s ",str);
return 0;
}
strupr() function
The strupr() function is used to convert lower case characters of string into upper case characters.
Syntax:
strupr (str);
where str is string to be converted into upper case characters.
Program 50 Write a program to input string in array and display them.
#include <stdio.h>
#include<string.h>
int main()
{
char str[50];
printf("Enter a string : ");
scanf("%s",str);
strupr(str);
printf("Given string in upper case is %s ",str);
return 0;
}
Worked Out Examples
Program 51 Write a program to find the length of the given string without using built in function.
#include <stdio.h>
#include<string.h>
int main()
{
char str[50];
int i,length=0;
printf("Enter a string : ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)// no body so use semicolon
{
length++;
}
printf("Length of string is %d",length);
return 0;
}
Program 52 Write a program to input strings and sort them.
#include<stdio.h>
#include<string.h>
int main()
{
char string[100][100],temp[100];
int i,j,n;
printf("enter n as total strings\n");
scanf("%d",&n);
printf("enter strings\n");
for(i=0;i<n;i++)
{
printf(" string for location %d = ",i);
scanf("%s",string[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(string[i],string[j])>0)
{
strcpy(temp,string[i]);
strcpy(string[i],string[j]);
strcpy(string[j],temp);
}
}
}
printf("sorted strings are\n");
for(i=0;i<n;i++)
{
printf("name =%s \n",string[i]);
}
return 0;
}
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...