C Programming - String

C Programming - String

Definition: The collections of the characters are called string. It is always enclosed in double
quotation marks. It contains characters, symbols, numbers etc.
Example: “Apple”, “305030”etc.
String Handling/ manipulation Functions:
The functions which are used to interpret the strings operations such as counting the length of
string, converting string from lower case to upper case and vice-versa etc is called string
manipulation/handling functions.
The c library supports many string handling functions that can be used to carry out many of
the string manipulation. The header file “string.h” is used for string manipulation functions.

1. Strlen(variable name): - This string function is used to find out the exact length of the
string. The return of the strlen function is integer value.
Syntax:
n= strlen(str);
where str is the string and n is the length of string , returned by strlen function
Example:
43. WAP to count the length of string?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int l;
char str[100];
printf("Enter a string");
scanf("%s",str);
l=strlen(str);
printf("LENGTH=%d",l);
getch();
}

Output:
Enter a string NEPAL
LENGTH=5

2. Strcat(destination,source):- This string function is used to concatenate / merge two
strings in first one.
Syntax: strcat(str1,str2); where str1,str2 are the two strings to be join together.
Example:
44. WAP to merge two strings?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[100],b[100];
printf("Enter string1,string 2");
scanf("%s%s",a,b);
strcat(a,b);
printf("After concatenating two string
=%s",a);
getch();
}

Output:
Enter string1,string2
Kathmandu
Nepal
After concatenating two string
=KathamdnuNepal

3. Strcmp(string1,string2):- This is used to compare two string each other.
Syntax:
strcmp(str1,str2);
Where str1m str2 are two strings to be compared.
Example:
45. WAP to compare two strings?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{

Enter string1,string 2Nepal
Nepal
Both strings are same.
//Enter string1,string 2 Nepal

char a[100],b[100];
printf("Enter string1,string 2");
scanf("%s%s",a,b);
if(strcmp(a,b)==0)
printf("Both strings are same.");
else
printf("Both strings are not same.");
getch();
}

Kathmandu
Both strings are not same.

4. strrev(variable name):- This is used to reverse string.
Syntax:
strrev(str);
where str is the string to be reverse in order.
Example:
46. WAP to convert string into reverse order?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
printf("Enter a string");
scanf("%s",str);
printf("REVERSE ORDER=%s",strrev(str));
getch();
}

Output:
Enter a string KTM
REVERSE ORDER=MTK

5. Strupr(string):- This is used to change case of characters.
Syntax: Struprr(str); Where str is string to be converted into uppercase.
Example:
47. WAP to convert string into upper case?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
printf("Enter a string");
scanf("%s",str);
printf("UPPER CASE=%s",strupr(str));
getch();
}

Output:
Enter a string nepal
UPPER CASE=NEPAL

6. Strlwr(string):- This is used to change case of characters.
Syntax:
Strlwr(str);
Where str is string to be converted into lowercase.
Example:
48. WAP to convert string into lowercase?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
printf("Enter a string");
scanf("%s",str);
printf("lower case=%s",strlwr(str));
getch();
}
Output:
Enter a string NEPAL
lower case=Nepal

7. Strcpy (str1,str2):- It is used to copy one string into other string.
Syntax: strcpy(str1,str2); Where str1,str2 are two strings and content of string2 is copied
on a string str1. Example:
49. WAP to copy string ?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[100],str2[100];
printf("Enter a first string=");
scanf("%s",str1);
strcpy(str2,str1);
printf("After copying the string
=%s",str2);
getch();
}

Output:
Enter a first string=Nepal
After copying the string =Nepal

Homework Questions

1. Describe any five string manipulation function in C. Explain strcpy and strcmp with
     examples.
2. Describe any five “string handling functions” with examples.
3. What is string? Explain any four string handling functions with example.
4. WAP to read a line of text and to convert it into uppercase.
5. What do you mean by string manipulation function ? Explain about strcpy and strcat?