1) strlen() in C :
len = strlen(str);
printf("\n Length of the Given String is : %d",len);
getch();
}
This function is used to determine the length of a given
string.
Syntax :
variable=strlen(“string”);
The string length will be returned to the variable.
Sample Program
str_len.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50];
int len;
printf("Enter a string : ");
scanf("%s",&str);
#include<conio.h>
#include<string.h>
void main()
{
char str[50];
int len;
printf("Enter a string : ");
scanf("%s",&str);
len = strlen(str);
printf("\n Length of the Given String is : %d",len);
getch();
}
Output:
Enter a string : Hello
Length of the Given String is : 5
2) strcat() in C :
This function is used to join (concatenate) two strings.
Syntax :
strcat(string1,string2);
Sample program :
str_cat.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[50], str2[50];
printf("Enter first string : ");
scanf("%s",&str1);
#include<conio.h>
#include<string.h>
void main()
{
char str1[50], str2[50];
printf("Enter first string : ");
scanf("%s",&str1);
printf("Enter second string :
");
scanf("%s",&str2);
scanf("%s",&str2);
strcat(str1,str2);
printf("Concatinated String : %s",str1);
getch();
}
printf("Concatinated String : %s",str1);
getch();
}
Output:
Enter first string : Good
Enter second string : Morning
3) strcmp() in C :
This function is used to compare two strings. They are case
sensitive.
Syntax :
strcmp(string1,string2);
If both the strings are equal return value will be 0, else
non_zero value will be returned.
Sample program :
str_cmp.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char
str1[50],str2[50];
int n;
printf("Enter
string one : ");
scanf("%s",&str1);
printf("Enter
string two : ");
scanf("%s",&str2);
n=strcmp(str1,str2);
if(n==0)
{
printf("Both
the Strings are Equal");
}
else
{
printf("Strings
are not Equal");
}
getch();
}
Output :
Enter string one :Hello
Enter string two :Hello
4) strcpy() in C :
This function copies the string from one variable to another
variable.
Syntax :
strcpy(source_variable, destination_variable);
Sample Program
str_cpy.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[50], str2[50];
printf("Enter a string :
");
scanf("%s",&str1);
strcpy(str2,str1);
printf("Value
of str1 : %s",str1);
printf("\nValue
of str2 : %s",str2);
getch();
}
Output :
Enter a string : Good Morning
Value of str1 : Good Morning
5) strrev() in C :
This function is sued to reverse the characters in a given
string.
Syntax :
strrev(string);
Sample program :
str_rev.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char
str[50];
printf("Enter
a string : ");
scanf("%s",&str);
strrev(str);
printf("Reverse
String : %s",str);
getch();
}
Output:
Enter a string : hello
6) strset() in C :
This function replaces all the characters of a string with a
given symbol or character.
Syntax :
strset(string,symbol);
Sample program :
str_set.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char
str1[30],str2;
printf("Enter
a string: ");
gets(str1);
printf("Enter
a symbol to replace the string : ");
scanf("%c",&str2);
strset(str1,str2);
printf("After
strset : %s",str1);
getch();
}
Output:
Enter a string: Good
Enter a symbol to replace the string : *
No comments:
Post a Comment