spliting string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 123scope
    New Member
    • Mar 2010
    • 12

    spliting string.

    Hi readers, I have got a string
    a[ ] = {Name surname ...... };

    I want to split the string into 2 the first string will contain the Name and the
    second string will contain the rest after the surname, can anyone please
    suggest how to do this.

    thx
  • myusernotyours
    New Member
    • Nov 2007
    • 188

    #2
    It depends on how the string is presented. I assume you are using C and Name and Surname are separated by a space.

    You could loop through the characters in the string starting at index 0. and search for the space character. Meanwhile you are storing the characters you find in another string. Once you find a space character, you have the Name. By continuing to the end of the string which is denoted by the Null terminator i.e '\0' you will have the surname.

    By the way, a string in C is just an array of characters thats why you are able to loop through them.

    Regards,

    Alex.

    Comment

    • 123scope
      New Member
      • Mar 2010
      • 12

      #3
      here is my code, can you find any mistakes please??? I cannot chop from the second word.

      OUTPUT
      Input: hello there

      hello there
      hello there
      hello there
      hello there
      h
      e
      l
      l
      o

      t
      h
      e
      r
      e


      h e l l o => first string
      Segmentation fault (core dumped)

      =============== =============== =====
      #include <stdio.h>
      #include <string.h>

      int main(int argc, char *argv[])
      {
      char b[]={};
      char b2[]={};
      char b3[]={};
      char searchword[]={};
      char wordmary[]={};
      int i=0;
      int j;
      fgets(b, 15, stdin);
      //printf("%s", b);

      for(i=0; i<=strlen(b); i++){
      b2[i]=b[i];
      b3[i]=b[i];
      }

      printf("%s", b);
      printf("%s", b2);
      printf("%s", b3);

      printf("%c\n", b[0]);
      printf("%c\n", b[1]);
      printf("%c\n", b[2]);
      printf("%c\n", b[3]);
      printf("%c\n", b[4]);
      printf("%c\n", b[5]);
      printf("%c\n", b[6]);
      printf("%c\n", b[7]);
      printf("%c\n", b[8]);
      printf("%c\n", b[9]);
      printf("%c\n", b[10]);
      printf("%c\n", b[11]);


      for(i=0; i<=5; i++){
      printf("%c ", b3[i]); //1st print
      }
      printf("\n");

      for(i=0; i<=5; i++){
      searchword[i]=b3[i];
      }
      searchword[6]='\0';


      for(j=7; j<='\0'; j++){
      for(i=0; i=='\0'; i++){
      wordmary[i]=b3[j];
      printf("%c\n", wordmary[j]);
      }
      }
      //printf("%s\n", searchword);


      return 0;
      }

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        You are not searching for a space as myusernotyours suggested.
        And the code is littered with magic numbers which makes reading difficult.

        As this is a char string can you make use of strtok?

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          for(j=7; j<='\0'; j++){
          I think you mean the character at index j is not '\0'

          Comment

          • endazyar
            New Member
            • Mar 2010
            • 11

            #6
            i think you should use strncpy or strtok functions..Othe rwise your implementation will be too much complicated.

            Comment

            Working...