strlen(), strcpy(), strcat(), strcmp()
strlen()
Program to demonstrate the use of strlen() function
strlen()
strlen() stands for string length. This function is used to find a number of characters stored in a string variable or string constant. This function doesn’t count the null character.
The header file required for this function is “string.h”--
#include<stdio.h>
#include<string.h>
int main()
{
char studentname[20]=”Aryan”;
int n;n=strlen(studentname);printf(“\nNumber of characters=%d”,n);
return(0);
}
#include<string.h>
int main()
{
char studentname[20]=”Aryan”;
int n;n=strlen(studentname);printf(“\nNumber of characters=%d”,n);
return(0);
}
Output
Number of characters=5
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 wants to store the value.
Str_Source is the string value that 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=”Aryan”;
name=”Aryan”;
In place of assignment operator =, we need to use strcpy() function.
char name[20];
strcpy(name,”Aryan”);
strcpy(name,”Aryan”);
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);
}
#include<string.h>
int main()
{
char studentname[20];
strcpy(studentname,”Amit”);
printf(“\nStudentname=%s”,studentname);
return(0);
}
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. The 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]=”Aryan”;
clrscr();
strcat(studentname,” Singh”);
printf(“\nStudentname=%s”,studentname);
return(0);
}
#include<string.h>
int main()
{
char studentname[20]=”Aryan”;
clrscr();
strcat(studentname,” Singh”);
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Studentname=Aryan Singh
strcmp()
strcmp() stands for string compare. This function is used to compare the value of one string value with another string value.
The header file required for this function is “string.h”.
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);
}
#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
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें