C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

String Handling Functions:

In C programming, string handling functions are provided by the standard library string.h. These functions allow you to manipulate strings efficiently. 

Some commonly used string handling functions:

  1. strlen():

strlen() is used to find the length of the string. String handling function strlen() returns the number of characters in string.

Syntax:

integer_variable = strlen(string or string_variable);

Example :

#include<stdio.h>

#include<string.h>


int main()

{

 char str[30];

 int len;

 printf("Enter string:\n");

 gets(str);

 len = strlen(str);

 printf("Length of given string is: %d", len);

 return 0;

}
  1. strcpy():

strcpy() is used to copy content of one string variable to another string variable i.e. strcpy(string2, string1) copies content of string1 to string2.

Syntax: 

strcpy ( string2, string1);

Example:

#include<stdio.h>

#include<string.h>

int main()

{

 char str1[30], str2[30];

 printf("Enter string:\n");

 gets(str1);

 strcpy(str2, str1);

 printf("Coped string is: %s”, str2);

 return 0;

}
  1. strcat():

strcat() is used to concatenate two strings. Concatenation is the process of merging content of one string to another string. Function strcat(str1, str2) concatenates content of string str2 after content of string str1.

Syntax:

strcat( string1, string2);

Example 

#include<stdio.h>

#include<string.h>

int main()

{

 char str1[50], str2[50];

 printf("Enter first string:\n");

 gets(str1);

 printf("Enter second string:\n");

 gets(str2);

 strcat(str1,str2);

 printf("Concatenated string is: %s", str1);

 return 0;

}
  1. strcmp():

strcmp() is used to compare two strings. This function returns 0 if two strings are same otherwise it returns some integer value other than 0.

Syntax:

integer_variable = strcmp( string1, string2);

Example 

#include<stdio.h>

#include<string.h>

int main()

{

 char str1[40], str2[40];

 int d;

 printf("Enter first string:\n");

 gets(str1);

 printf("Enter second string:\n");

 gets(str2);

 d = strcmp(str1, str2);

 if(d==0)

 {

  printf("Given strings are same.");

 }

 else

 {

  printf("Given strings are different.");

 }


 return 0;

}
  1. Strrev()

strrev() is used to reverse string in C programming language. Function strrev(str1) reverses the content of string str1.

Syntax:

strrev(string);

Example 

#include<stdio.h>

#include<string.h>

int main()

{

char name[40];

printf("Enter your name: ");

gets(name);

strrev(name);

printf("Reversed name is: %s", name);



  return 0;

}
  1. strupr()

strupr() is used to convert all lower case letter in string to upper case i.e. strlwr(str) converts all lower case letter in string str to upper case.

Syntax:

strupr( string);

Example:

#include<stdio.h>

#include<string.h>

int main()

{

 char str[40];

 printf("Enter string:\n");

 gets(str);

 strupr(str);

 printf("String in uppercase is:");

 puts(str);

 return 0;

}
  1. strlwr()

strlwr() is used to convert all upper case letter in string to lower case i.e. strlwr(str) converts all upper case letter in string str to lowercase.

Syntax:

strlwr( string);

Example:

#include

#include<string.h>

int main()

{

 char str[40];

 printf("Enter string:\n");

 gets(str);

 strlwr(str);

 printf("String in lowercase is:");

 puts(str);

 return 0;

}