Handling of strings


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.
         For example, The word is Whereisstuff
         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

Example:

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);

Example program strlen():

#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();
}

Output:
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);

Example program strcpy():

#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();
}

Output:
3. The strcat() function:
It is used to concatenate or combine, two strings together and forms a new string.
Syntax:

strcat(string1,string2);

Example program strcat():

#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();
}

Output:
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);

Example program strcmp():

#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();
}

Output:
if condition:
else condition:
5. The strrev() function;
It is used to reverse a string.
Syntax:

strrev(string);

Example program strrev():

#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();
}

Output:
6. The strlwr() function:
It is used convert string into lower case(small letters).
Syntax:

strlwr(string);

Example program strlwr():

#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();
}

Output:
7. The strupr() function:
It is used convert string into upper case(capital letters).
Syntax:

strupr(string);

Example program strupr():

#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();
}

Output:

8. The stricmp() function:
It is used to compare two strings, no difference between small and capital letters.

Syntax:

stricmp(string1,string2);

Example program stricmp():

#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();
}

Output:
if condition:
else condition:
Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me