String Function
In C programming, a string function is a built-in library function designed to manipulate, manage, and perform operations on text. It is used to find the length of a given string, reverse the string, copy the string, change into upper case or lower case, compare the two strings, etc.
There are 7 string functions. They are:
strlen()
strrev()
strcat()
strupr()
strlwr()
strcmp()
strcpy()
1. strlen()
This string function is used to find the length of string. Its keyword is strlen().
For example:
#include<stdio.h>
#include<string.h>
int main()
{
int x;
char word[50];
printf("enter a word ");
scanf("%s", word);
x = strlen(word);
printf("The length of word is %d ", x);
return 0;
}
Output:
enter a word prapti
The length of word is 6
2. strrev()
This string function is used to reverse the given string.
For example:
#include<stdio.h>
#include<string.h>
int main()
{
char word[25];
printf("enter a word\n ");
scanf("%s", word);
strrev(word);
printf("The reversed word is %s", word);
return 0;
}
Output:
Enter a word
prapti
The reversed word is itparp
3. strcat()
This string function is used to join two string together.
For example:
#include<stdio.h>
#include<string.h>
int main()
{
char word1[25], word2[25];
printf("enter two words\n");
scanf("%s %s", word1, word2);
strcat(word1, word2);
printf("The combined word is %s", word1);
return 0;
}
Output:
Enter two words
gita sita
The combined word is gitasita
4. strupr()
The string function is used to convert lower case into upper case letter.
For example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char word[25];
clrscr();
printf("enter a word\n");
scanf("%s", word);
strupr(word);
printf("The upper case letter is %s", word);
getch();
}
Output:
Enter a word
prapti
The upper case letter is PRAPTI
5. strlwr()
The string function is used to convert upper case into lower case letter.
For example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char word[25];
clrscr();
printf("enter a word\n");
scanf("%s", word);
strlwr(word);
printf("The lower case letter is %s", word);
getch();
}
Output:
Enter a word
PRAPTI
The lower case letter is prapti
6. strcmp()
This string function is used to compare two string.
For example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char word1[25], word2[25];
clrscr();
printf("enter two words\n");
scanf("%s %s", word1, word2);
if (strcmp(word1, word2) == 0)
{
printf("Both string are same");
}
else
{
printf("Both string are not same");
}
getch();
}
Output:
Enter two words
prapti, prapti
Both string are same
7. strcpy()
This string function is used to copy string from one string variable to another variable.
For example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char word1[25], word2[25];
clrscr();
printf("enter a word\n");
scanf("%s", word2);
strcpy(word1, word2);
printf("The copied word is %s", word1);
getch();
}
Output:
enter a word
prapti
The copied word is prapti
Practice Questions
Q. WAP to check whether the given word is palindrome or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char word1[25], word2[25];
clrscr();
printf("enter a word\n");
scanf("%s", word1);
strcpy(word2, word1);
strrev(word1);
if (strcmp(word1, word2) == 0)
{
printf("palindrome");
}
else
{
printf("Not palindrome");
}
getch();
}
Output:
Enter a word
prapti
Not palindrome


No comments:
Post a Comment