Monday 14 October 2013

Operation on string

Operation on string is not like operation o integer. for an example
we can't assign data or compare directly
 str1=str1+str3
or
str1==str2
BOTh assignment are wrong .So we use new way to do operation on string.

comparison of two string : two way to do this one is very obvious i.e compare character by character. using while or for loop.OR second way is too easy using function strcmp()   .

One important property of strcmp() is It has volue 0 if string compare are equal Else it has value equal to numeric difference between first nonmatching character in the string. 


 #include<stdio.h>  
 #include<string.h>  
 main()  
 {  
      char str1[30],str2[30];  
      printf("\n insert string 1 :");  
      gets(str1);  
      printf("\n insert string 2 :");  
      gets(str2);  
      if(strcmp(str1,str2))  // comparison set to 0 i.e false if str1==str2
      {  
           printf("\n str1 != str2");  
      }  
      else  
       printf("\nstr1 & str2 is equal");  
 }  
OUtPUT:- 

Adding to string : there is also two way one is basic using for loop or while loop . another is using function strcat() simply take 2 string as input & append string 2 at end of string one. nesting of this funtion is also allowed.

 #include<stdio.h>  
 #include<string.h>  
 main()  
 {  
      char str1[30],str2[30];  
      printf("\n insert string 1 :");  
      gets(str1);  
      printf("\n insert string 2 :");  
      gets(str2);  
      strcat(str1,str2);   // appending str2 atend of str1
      printf("\n new string is :");  
      puts(str1);  
 }  
OUTPUT :

Strcpy() is also a function similar to strcat() but difference is it delete previous data of string1 & assign data of string2 to string one.

 #include<stdio.h>  
 #include<string.h>  
 main()  
 {  
      char str1[30],str2[30];  
      printf("\n insert string 1 :");  
      gets(str1);  
      printf("\n insert string 2 :");  
      gets(str2);  
      strcpy(str1,str2);  
      printf("\n new string is :");  
      puts(str1);  
 }  
you can check out by yourself.

To find length of string: two way one is as obvious using while or for loop. secondary using strlen() function.

we show its demo in next progarm.

strncpy() : it is similar to strcpy() but there is only one difference : strncpy() copy only n char from left.

 #include<stdio.h>  
 #include<string.h>  
 main()  
 {  
      char str1[30],str2[30];  
      int len;  
      printf("\n insert string 1 :");  
      gets(str1);  
      printf("\n insert string 2 :");  
      gets(str2);  
      strncpy(str1,str2,5); //requred operation adding 5 digit from str2 to first five digit of str1  
      printf("\n new string is :");  
      puts(str1);  
      len=strlen(str1); // to find length  
      printf("\n length of string 1 is :=%d",len);  
 }  
OUTPUT :-

2 comments:

  1. thanks dude its vey good .....thanks again

    ReplyDelete
  2. Hello there! This is my first comment here so I just wanted to give a quick shout out
    and tell you I genuinely enjoy reading through your posts.
    Can you recommend any other blogs/websites/forums that cover the same topics?

    Thank you so much!

    Also visit my web site :: braces tampa; ,

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets