String
manipulation:
- In the ‘C’ language, the group of characters, digits, and symbols enclosed within quotation marks are called as a string, otherwise strings are an array of characters.
- The string is nothing but, it is a collection of character or words.
Here, w, h, e, r, e, i, s, t, u, f, f such as characters and Whereisstuff as string
- The null character (‘\0’) is used to mark the end of the string.
Syntax:
char name[]=”String”; //initialization
or
char name[Set maximum length of the string];//declaration
char name[]=”String”; //initialization
or
char name[Set maximum length of the string];//declaration
Example:
char name[]=”whereisstuff”;
or
char name[20];
char name[]=”whereisstuff”;
or
char name[20];
String
handling function:
Most widely used functions are,
1.
The strlen() function:
It is used to count and return the number of
characters presents.
Syntax:
Variable_name=strlen(string);
Variable_name=strlen(string);
Example program strlen():
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
char a[30];
char
b[]="whereisstuff";
int len=strlen(a);
clrscr();
printf("Enter a
string: \n");
scanf("%s",&a);
printf("The length
of %s string is: %d\n",a,len);
printf("String
length of %s is %d \n",b,strlen(b));
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char a[30];
char
b[]="whereisstuff";
int len=strlen(a);
clrscr();
printf("Enter a
string: \n");
scanf("%s",&a);
printf("The length
of %s string is: %d\n",a,len);
printf("String
length of %s is %d \n",b,strlen(b));
getch();
}
2.
The strcpy() function:
It is used to copy the content of one string to
another and it almost works like a string assignment operator.
Syntax:
strcpy(string1,string2);
strcpy(string1,string2);
Example program strcpy():
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[30];
char
str2[]="whereisstuff";
clrscr();
strcpy(str1,str2);
printf("\n
Original string is %s",str2);
printf("\n Copied
string is %s",str1);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[30];
char
str2[]="whereisstuff";
clrscr();
strcpy(str1,str2);
printf("\n
Original string is %s",str2);
printf("\n Copied
string is %s",str1);
getch();
}
3.
The strcat() function:
It is used to concatenate or combine, two strings
together and forms a new string.
Syntax:
strcat(string1,string2);
strcat(string1,string2);
Example program strcat():
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
char
str1[]="whereisstuff";
char
str2[]=".com";
clrscr();
printf("\n
Concatenate string is %s", strcat(str1,str2));
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char
str1[]="whereisstuff";
char
str2[]=".com";
clrscr();
printf("\n
Concatenate string is %s", strcat(str1,str2));
getch();
}
4.
The strcmp() function:
It is used to compare two strings to find out
whether they are the same or different.
Syntax:
strcmp(string1,string2);
strcmp(string1,string2);
Example program strcmp():
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("\n Enter two string: ");
scanf("%s
%s",&str1,&str2);
if
(strcmp(str1,str2)==0)
{
printf("\n Compared string is right because
two strings are the same case.");
}
else
{
printf("\n Compared string is wrong because
two strings are different case");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("\n Enter two string: ");
scanf("%s
%s",&str1,&str2);
if
(strcmp(str1,str2)==0)
{
printf("\n Compared string is right because
two strings are the same case.");
}
else
{
printf("\n Compared string is wrong because
two strings are different case");
}
getch();
}
if condition:
else condition:
5.
The strrev() function;
It is used to reverse a string.
Syntax:
strrev(string);
strrev(string);
Example program strrev():
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
printf("\n
Enter a string: ");
scanf("%s",&str1);
printf("\n The reversed string is %s",strrev(str1));
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
printf("\n
Enter a string: ");
scanf("%s",&str1);
printf("\n The reversed string is %s",strrev(str1));
getch();
}
6.
The strlwr() function:
It is used convert string into lower case(small letters).
Syntax:
strlwr(string);
strlwr(string);
Example program strlwr():
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
printf("\n Enter a string in capital letters ");
scanf("%s",&str1);
printf("\n Lower
string is %s", strlwr(str1));
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
printf("\n Enter a string in capital letters ");
scanf("%s",&str1);
printf("\n Lower
string is %s", strlwr(str1));
getch();
}
7.
The strupr() function:
It is used convert string into upper case(capital
letters).
Syntax:
strupr(string);
strupr(string);
Example program strupr():
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
printf("\n Enter a string in small letters ");
scanf("%s",&str1);
printf("\n Upper
string is %s", strupr(str1));
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20];
clrscr();
printf("\n Enter a string in small letters ");
scanf("%s",&str1);
printf("\n Upper
string is %s", strupr(str1));
getch();
}
8.
The stricmp() function:
It is used to compare two strings, no difference
between small and capital letters.
Syntax:
stricmp(string1,string2);
stricmp(string1,string2);
Example program stricmp():
Output:
if condition:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("\n Enter two string: ");
scanf("%s
%s",&str1,&str2);
if
(stricmp(str1,str2)==0)
{
printf("\n Compared string is right because
two
strings are the same.");
}
else
{
printf("\n Compared string is wrong because
two strings are different.");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
clrscr();
printf("\n Enter two string: ");
scanf("%s
%s",&str1,&str2);
if
(stricmp(str1,str2)==0)
{
printf("\n Compared string is right because
two
strings are the same.");
}
else
{
printf("\n Compared string is wrong because
two strings are different.");
}
getch();
}
else condition:
No comments:
Post a Comment