String In C Language

 INTRODUCTION

 Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a unique character '\0' (null pointer : represent end of string) .String is a sequence of characters stored in a character array. It is a text enclosed in double quotation mark.

Declaration 

  1. char str[ ]={'A','B','C','D','\0'};
  2. char str[ ]="ABCD";
  3. String operation performed using "string.h" header file.
  4. String I/O = puts(instead of printf) : ouput  ,  gets(instead of scanf) : input
Specifier is : %s

Example :
#include<stdio.h>
#include<string.h>
int main()
{
int a,length;
char str1[50];
char str2[50];
printf("enter str1 :");
gets(str1);
printf("enter ster2 :");
gets(str2);
a=strcmp(str1,str2);
if(strcmp(str1,str2)==0)
{
length=strlen(str1);
printf("strings are equal and length %d \n",length);
}
if(strcmp(str1,str2)>0)
{
printf("string 1 is greater \n");
printf("string 1 uppercase %s \n",strupr(str1));
printf("string 2 lowercase %s \n",strlwr(str2));
}
else
{
printf("string 2 is greater \n");
printf("string 2 in uppercase is %s \n",strupr(str2));
printf("string 1 in lowercase is %s \n",strlwr(str1));
}
}

output :
enter str1 :INDIA
enter ster2 :maharashtra
string 2 is greater
string 2 in uppercase is MAHARASHTRA
string 1 in lowercase is india


String Commands :

FUNCTION

DESCRIPTION

Strlen()

Can compute the length of the string

Strcpy()

Can copy the content of a string to another

Strcat()

Is used to concatenate or join two strings

Strcmp()

Can compare two strings

Strlwr()

Can convert the string to lowercase

Strupr()

Is used to convert the letter of string to uppercase

Strrev()

Is used to reverse the string

1) Example of  Strlen() :

code :

#include<stdio.h>

#include<string.h>

int main()

{

       char str[20];

int a;

printf("enter a string : \n");

scanf("%s",str);

a=strlen(str);

printf("%d \n",a);

printf("%d ",sizeof(str));

}

output :

enter a string :

1234                                                       //I entered value 1234

4                                                             //length of string

20                                                           //sizeof of the string which is mentiond in str[]


2) Example of  Strcpy() :

code :

#include<stdio.h>

#include<string.h>

int main()

{

char str1[21];

char str2[12];

printf("enter string \n");

scanf("%s",str1);

strcpy(str2,str1);

printf("%s",str2);

}

output :

enter string

1245                                    //entered string one

1245                                    //which is stored in string two


3) Example of  Strcat() :

code :

#include<stdio.h>

#include<string.h>

int main()

{

char str1[21];

char str2[30];

printf("enter str1 \n");

scanf("%s",str1);

printf("enter str2 \n");

scanf("%s",str2);

strcat(str1,str2);

printf("%s",str1);

}

output :

enter str1

1234

enter str2

2345

12342345                                            //string one and two are joint


4)Example of  Strcmp() :

code :

#include<stdio.h>

#include<string.h>

int main()

{

char str[20];

char string[12];

int a;

printf("enter any str \n");

scanf("%s",str);

printf("enter any string \n");

scanf("%s",string);

a=strcmp(str,string);

printf("%d %d \n",sizeof(str),sizeof(string));

printf("%d",a);

}

output :

enter any str

1234                                               //string one length is 4

enter any string

123456678                                    //string two length is 9

20 12                                             //sizeof of two strings

-1                                                  //when string 2 > string 1 it will show -1 & string 1>string 2 it will                                                               show 1


5)Example of  Strlwr() :

code :

#include<stdio.h>

#include<string.h>

int main()

{

char str[21];

printf("enter string \n");

scanf("%s",str);

printf("conversion into lowercase \n");

strlwr(str);

printf("%s",str);

output :

enter string

ABC                                                         

conversion into lowercase

abc                                                           


6)Example of  Strupr() :

code :

#include<stdio.h>

#include<string.h>

int main()

{

char str[21];

printf("enter string \n");

scanf("%s",str);

printf("conversion into uppercase \n");

strupr(str);

printf("%s",str);

}

output :

enter string

india                                                           

conversion into uppercase

INDIA


7)Example of  Strrev() :

code :

#include<stdio.h>

#include<string.h>

int main()

{

char str[21];

printf("enter string :\n");

scanf("%s",str);

strrev(str);

printf("reverse string :\n");

printf("%s",str);

}

outpur :

enter string :

abcdef

reverse string :

fedcba
















No comments:

Post a Comment