String functions in c language with examples | strcpy, strcat, strcmp, strrev, strlen

Preview
You must first complete Strings in c language with examples before viewing this Lesson

 String functions in c language with examples

There are various string functions which we can use in C Language. We need to include header file string.h in our program to use these functions in our program.

Various string functions in c language are:

  • strcpy()
  • strncpy()
  • strcat()
  • strlen()
  • strrev()
  • strcmp()
  • strcmpi()
  • strncmp()
  • strlwr()
  • strupr()

1. strcpy()

strcpy() stands for string copy. This function is used to copy value of one string variable or string constant in another string variable.

The header file required for this function is “string.h”.

The syntax for strcpy() is

strcpy(Str_Target,Str_Source);

Str_Target is the string variable in which want to store the value.

Str_Source is the string value which we want to store in string variable Str_Target. This value can be a string constant or a string variable. Size of Str_Target should be larger than or equal to the size of value to be stored in the variable.

We can’t assign value to a string variable directly as

char name[20];
name=”Amit”;

In place of assignment operator =, we need to use strcpy() function.

char name[20];
strcpy(name,”Amit”);

Program to demonstrate the use of strcpy() function.
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[20];
strcpy(studentname,”Amit”);
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Studentname=Amit

2. strncpy()

This function is used to copy specific number of characters from one string variable or string constant to another string variable.

The header file required for this function is “string.h”.

The syntax for strncpy() is

strcpy(Str_Target,Str_Source,N);

Str_Target is the string variable in which want to store the value.

Str_Source is the string value from which we want to store specific characters in string variable Str_Target. This value can be a string constant or a string variable. Size of Str_Target should be larger than or equal to the size of value to be stored in the variable.

N specifies the number of characters to be copied from Str_Source to Str_Target.

Program to demonstrate the use of strncpy() function.
#include<stdio.h>
#include<string.h>
int main()
{
char name[30]=”Lovejot”;
char name1[30];
strncpy(name1,name,3);  /*String copy*/

name1[3]=’\0′;

printf(“\nName=%s”,name1);
return(0);
}

Output
Name=Lov

3. strcat()

strcat() stands for string concat. This function is used to combine values of two string variables together.

The header file required for this function is “string.h”.

The syntax for strcat() function is

strcat(Str_Target,Str_Source);

Str_Target is the string variable whose value we want to combine with some other string value.

Str_Source is the string value which we want to combine with the value of string variable

Str_Target. This value can be a string constant or a string variable. Size of Str_Target should be larger than or equal to the combined size of Str_Target and Str_Source.

Program to demonstrate the use of strcat() function.
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[20]=”Amit”;
clrscr();
strcat(studentname,” Singh”);
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Studentname=Amit Singh

Description of above program

In above program, string variable studentname contains value “Amit”. After applying strcat() function, its value will be concatenated with “ Singh” and the final value of string variable studentname would become Amit Singh

4. strlen()

strlen() stands for string length. This function is used to find number of characters stored in a string variable or string constant.

This function doesn’t count null character.

The header file required for this function is “string.h”.

The syntax for strlen() function is:

strlen(Str_Var);

Str_Var is the string value whose length we want to find.

Program to demonstrate the use of strlen() function.
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[20]=”Amit”;
int n;n=strlen(studentname);

printf(“\nNumber of characters=%d”,n);
return(0);
}

Output
Number of characters=4

Description of above program

In above program, string variable studentname contains value “Amit”. strlen() function finds number of characters in the value “Amit” in string variable studentname that  contains 4 characters. So strlen() would return 4.

 5. strrev()

strrev() stands for string reverse. This function is used to reverse the order of characters stored in a string variable i.e. first character becomes and last character and last character becomes the first character and so on.

The header file required for this function is “string.h”.

The syntax for strrev() function is

strrev(Str_Var);

Str_Var is the string variable whose value we want to reverse.

Program to demonstrate the use of strrev() function.
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[20]=”Amit”;
strrev(studentname);
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Studentname=timA

Description of above program

In above program, string variable studentname contains value “Amit”. strrev() function reverses the value. So value of studentname  becomes timA.

 6. strcmp()

strcmp()  stands for string compare. This function is used to compare value of one string value with another string value.

The header file required for this function is “string.h”.

The string values to be compared may be in the form of a string variable or string constant.

The value returned by this function depends upon following criteria:

  • Function returns 0 if the values of both the strings being compared are same.
  • If the value of first string value is larger than second string value, it would return a negative value.
  • For first string value to be smaller than second string value, it would return a positive value.

The syntax for strcmp() is

strcmp(Strvar1,Strvar2);

Here, Strvar1 and Strvar2 are string variables or constants which we want to compare.

Program to demonstrate the use of strcmp() function.
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[25];
strcpy(studentname,”Amita”);
if(strcmp(studentname,”Amit”)= =0)
cout<<”\nWelcome”;
else
cout<<”\nBye”;
return(0);
}
Output
Bye

Description of above program

In this program, string variable studentname has been assigned value “Amita”. Further in the program, value of string variable studentname has been compared with the string value Amit.

If the value of studentname matches Amit it would show Welcome.

But in the program, the value of studentname doesn’t match Amit so the output would be Bye.

 7. strcmpi()

strcmp()  stands for string compare ignore case. This function is used to compare value of one string value with another string value but it does not care for the case of characters i.e. capital letters and small letters are assumed same.

The header file required for this function is “string.h”.

The string values to be compared may be in the form of a string variable or string constant.

The value returned by this function depends upon following criteria:

  • If the values of both the strings being compared are same, then .
  • For value of first string value larger than second string value, it would return a negative value.
  • In case, the value of first string is smaller than second string , it would return a positive value.

The syntax for strcmpi() is

strcmpi(Strvar1,Strvar2);

Here, Strvar1 and Strvar2 are string variables or constants which we want to compare.

Program to demonstrate the use of strcmpi() function.
#include<stdio.h>
#include<string.h>
int main()
{
char name1[20],name2[20];
printf(“Enter first name=”);
gets(name1);
printf(“Enter second name=”);
gets(name2);
if(strcmpi(name1,name2)==0)
printf(“Both names are same”);
else
printf(“Names are not same”);
return(0);
}
Output
Enter first name=lovejot
Enter second name=LOVEJOT
Both names are same

Description of above program

In this program, string variable name1 has been provided value lovejot” and name2 has been provided value LOVEJOT. Further in the program, value of string variable name1 has been compared with the string value name2.

8. strncmp()

strcmp()  stands for string compare with specific number of characters. This function is used to compare value of one string value with another string value but it would compare specific number of characters as provided by the programmer or user.

The header file required for this function is “string.h”.

The string values to be compared may be in the form of a string variable or string constant.

The value returned by this function depends upon following criteria:

  • It return 0 if values of both the strings being compared are same..
  • In case if value of first string value is larger than second string value, it would return a negative value.
  • If the values of first string value is smaller than second string value, it would return a positive value.

The syntax for strncmp() is

strncmp(Strvar1,Strvar2,N);

Here, Strvar1 and Strvar2 are string variables or constants which we want to compare.

N specifies the number of characters to be compared.

Program to demonstrate the use of strncmp() function.
#include<stdio.h>
#include<string.h>
int main()
{
char name1[]=”amit”,name2[]= “amita”;
if(strncmp(name1,name2,3)==0)
printf(“Values are same”);
else
printf(“Values are not same”);
return(0);
}
Output
Values are same

Description of above program

In this program, string variable name1 has been provided value amit and name2 has been provided value amita. Further in the program, value of string variable name1 has been compared with the string value name2. First three characters from both strings have been compared.

9. strlwr()

strlwr() stands for string lower. This function is used to convert the uppercase letters in the string to lowercase letters.

The header file required for this function is “string.h”.

Syntax of strlwr() function is:

strlwr(Str_Var);

Str_Var is the string variable whose value we want to convert into lower case.

Program to demonstrate the use of strlwr() function.
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[20]=”AMIT”;
strlwr(studentname);
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Studentname=amit

Description of above program

In this program, string variable studentname has been assigned value “Amit”.  strlwr() function converts the value into small case i.e. amit.

10. strupr()

strupr() stands for string upper. This function is used to convert the small case letters in the string to uppercase letters.

Header file required for this function is “string.h”.

Syntax for strupr() function is:

strupr(Str_Var);

Str_Var is the string variable whose value we want to convert into upper case.

Program to demonstrate the use of strupr() function.
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[20]=”amit”;
strupr(studentname);
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Studentname=AMIT

Description of above program

In this program, string variable studentname has been assigned value “amit”. strupr() function converts the value into capital case i.e. amit.

Test Your Knowledge

Write C program for following:

  1. Program to read a name. If the name is “Ajay”, message “Welcome” should be displayed otherwise message “Good Bye” should be displayed.
  2. Program to read name and password of a user. If username is “Anil” and password is “Amit”. Sum of two numbers should be displayed otherwise an error message should be displayed.
  3. Program to read %age of a student. If the percentage is more than 35 then the string variable result should get the value “Pass” otherwise it should get the value “Detained”.

Write the output of the following programs:

1.

int main( )

{

char n[]=”Amit”;

printf(“%s”,n);

}

2.

int main( )

{

char ch[]=”Lovejot”;

view(ch);

}

void view(char c[])

{

printf(“\n%s”,c);

}

3.

int main( )

{

char ch[]=”Lovejot”;

printf(“\n%s”,ch);

view(ch);

printf(“\n%s”,ch);

}

void view(char c[])

{

strupr(ch);

}

Lesson tags: assigning a value to string value in c, reversing a string value in c, strcpy() function of c, string functions in c
Back to: C Programming Language