TechCSE: String

String



  String is a array of characters ,where charecters are null-terminated. It means a string
contains the character that make up athe string followed by  a null(‘\0’). You cana say a string is
a one dimensional array.
Exp:     HELLO
 In the above string “HELLO” we reprasent it in an array, and in the last of the array it end with a
end notation(\0).This how we reprasent a string.

  The string is defined as :
      Char a[10];
      a=”HELLO”;
      printf(“%s”,a);
10 is the size of the array including the null character.  By using “%s” it denotes. To get a string
as ainput you can use scanf(“%s”,a) or gets(a) function. Differsnce between those is scanaf()
does not support  the space as a character but in gets function you can use space as a
character. And to print the string you can use puts(a).
  There are several inbuilt functions like
strlen(s1)     Find the length of the string s1.
strcpy(s1,s2)    Copy s2 into s1.
strcmp(s1,s2)    Compare s2 with s1 and returns 0 if bothe are same,less than 0 if s1<s2,  


      greater than 0 if s2<s1.
ststrcat(s1,s2)    Concates s2 with s1.
strchr(s1,ch)    Returns a pointer to the first occurrence of the ch to s1.
strstr(s1,s2)    Returns a pointer to the first occurrences of the s2 in s1.

Program Code of strlen(s1) :
    char string_length(char s1){
    int i,c = 0;
    for(i=0;s1*i+!=’\0’,i++)
      c++;
    return c;
    }
Program code of strcpy(s1,s2) :
       char strcpy(char s1,char s2){

          int i;
 
           for (i=0; s1[i] != '\0'; ++i)

                   s2[i] =  s1[i];

                      s2[i] = '\0';

                     return s2;

    }

for (i = 0; s1[i] == s2[i]; i++)

[i] ==s2t[i]; i++)
if (s1[i] == '\0')
return 0;
return s1[i] – s2[i];
}

Program code of strcat(s1,s2):
    char *strcat(char *s1,char *s2){

        size_t i,j;

        for (i = 0; s1[i] != '\0'; i++);

        for (j = 0; s2[j] != '\0'; j++)

                                  s1[i+j] = s2[j];

        s2[i+j] = '\0';
 
        return s2;

    }

 Program code of strcmp(s1,s2) :


int strcmp(char *s1, char *s2){
int i;

No comments:

Post a Comment

Copyright © TechCSE Urang-kurai