Thread: Strings and tokenizing need help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    Question Strings and tokenizing need help

    hi all i am new to the programing world & specially C with no sources of help available

    i hope someone here would help

    i am writing a simple code to extract words from a string & printing it

    i have spent a lot of time trying to do so, yet i still have problems

    not to waist ur time this is the program requirement i am suppose to do


    start programming by developing and testing a simple C function. Specifically, write the function
    void getCmd (int *argc, char *argv[])

    which reads a command line from the standard input device (keyboard) and returns the
    number of words/tokens in that command line in the integer variable argc and pointers
    to the individual words/tokens in the character vector argv[]. Thus, argc should
    contain the number of strings in the command line (the program name and the
    arguments) and the array of character strings argv contains each of these strings —
    pointer to the program name in argv[0], pointer to the first parameter in argv[1],
    pointer to the second parameter in argv[2], etc. You should identify the words/tokens
    in a command line by searching for the space character(s) delimiting the words/tokens.
    In particular, you should not use any library function to extract the words/tokens in a
    character string.
    In order to systematically develop your program, first write the algorithm to perform
    the above in pseudo code form. Be sure to show the words in a command line are
    identified and how argc and argv[] are computed. Then implement your algorithm as
    the C function getCmd.

    Write a main program (and store it in the file getCmd.c) which calls your getCmd
    function to read a command line and print the command line, the value of argc and the
    words in the command line as shown below. For example, for the command line
    copy ~/Pract1/getCmd.c ~/Pract2/getCmd.c
    the output of your main program should be
    The command line ‘copy ~/Pract1/getCmd.c ~/Pract2/getCmd.c’ contains the following 3 words:
    copy, ~/Pract1/getCmd.c, ~/Pract2/getCmd.c

    Test your program with at least the following command lines.
    cd ~/Pract2
    copy ~/Pract/getCmd.c ~/Pract/getCmd.c
    copy copy
    cp getCmd.c getCmd.c
    del copy
    del getCmd.c
    ren copy copy.test
    ren getCmd.c getCmd.c


    This is the code i wrote so far it is a bit stupid but this is really my first shot @ C
    and what makes it worse is that i am not suppose to use any of the special library functions ;(.

    Code:
    #include <stdio.h>
    
    void getCmd (int *argc, char **argv[]){
      int endv;
      char pos[7];
      int p;
      int tokens;
      p=0;
    
    for (int l=0; *argv[l] != '\r';l++){
      if ( argv[l]=='\0'){
        p=p+1;
        pos[p]=l;
        tokens= tokens+1;
       
      }
     }
    
    endv = l;
    argc = tokens;
      int loc;
      loc=0;     
        (*argc) = 0;
    
        argv[(*argc)++] = userInput+(pos[loc]);
    
    loc++;
        argv[(*argc)++] = userInput+(pos[loc]);
    
    loc++;
        argv[(*argc)++] = userInput+(pos[loc]);
    }
    int main(){
    
        char userInput[256];
        
        printf("type a string:  ");
        fgets(userInput, 256, stdin);
        printf("The command line ' %s ' contains the following ", userInput);
    
        int i;
        int cmdc;
        char* cmdv[20];
    
        getCmd(&cmdc, cmdv);
        for(i=0;i<cmdc; i++){
    	printf("argument %d is %s\n", i, cmdv[i]);    
        }
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    i have updated the code and the program compiles but when i run it and type the values it gives me an error Segmentation fault (core dumped)
    Code:
    #include <stdio.h>
    
       char userInput[256];
    void getCmd (int *argc, char *argv[]){
      int endv;
      char pos[7];
      int p;
      int l;
      int tokens;
      p=0;
    
    for ( l=0;*argv[l]!='\r';l++){
      if ( *argv[l]=='\0'){
        p=p+1;
        pos[p]=l;
        tokens= tokens+1;
       
      }
     }
    
    endv = l;
    *argc = tokens;
      int loc;
      loc=0;     
        (*argc) = 0;
    
        argv[(*argc)++] = userInput+(pos[loc]);
    
        loc = loc+1;
        argv[(*argc)++] = userInput+(pos[loc]);
    
    loc++;
        argv[(*argc)++] = userInput+(pos[loc]);
    }
    
    int main(){
        
        printf("type a string:  ");
        fgets(userInput, 256, stdin);
        printf("The command line ' &#37;s ' contains the following ", userInput);
    
        int i;
        int cmdc;
        char* cmdv[20];
    
        getCmd(&cmdc, cmdv);
        for(i=0;i<cmdc; i++){
    	printf("argument %d is %s\n", i, cmdv[i]);    
        }
        return 0;
    
    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your indentation needs fixing.

    You are calling getCmd() with an uninitialized array that you are then accessing in the function.

    I would also recommend not using "l" as a variable - it's often confusingly similar to "1".

    p = p + 1 is easier to read and write as p++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    ok so i am now a bit confused
    the array or string userInput is used to in the function "fgets" to store the users input

    now i am trying to pass the array to the getCmd function to have it tokenizing the string i was pass en an empty one if i understood your reply correctly but when i try to pas the userInput array

    still geting the "Segmentation fault (core dumped)"

    Code:
    #include <stdio.h>
    
    //   char userInput[256];
    char *userInput;
       int tokens;
       int endv;
       char pos[7];
       int p;
       int m;
    
    void getCmd (int *argc, char* argv[]){
    
    *argc = tokens;
    
      int loc;
      loc=0;     
        (*argc) = 0;
    
        argv[(*argc)++] = userInput+(pos[loc]);
        loc++;
        userInput[(pos[loc])] = '\0';
    
        argv[(*argc)++] = userInput+((pos[loc])+1);
        loc++;
    
        userInput[(pos[loc])] = '\0';
        argv[(*argc)++] = userInput+((pos[loc])+1);
    }
    
    int main(){
        
        printf("type a string:  ");
        //    fgets(userInput, 256, stdin);
        fgets(userInput, 265, stdin);
    
      p=0;
      // this is used to find the 
      for ( m=0;userInput[m]!='\0' ;m++){
      if ( userInput[m]==' '){
        p++;
        pos[p]=m;
        tokens++;
       
      }
     }
        endv = m;
        int i;
        int cmdc;
        char* cmdv[20];
    
        getCmd(&cmdc, cmdv);
    
        int firstturn = 0;
        for(i=0;i<cmdc; i++){
    
          if( firstturn=0){
    	printf("The command line ' &#37;s ' contains the following %d words: ", userInput, tokens);
    	    firstturn++;
          }
          printf("'%s, ", userInput[i]);
        }
        return 0;
    
    }

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    oops
    Last edited by Dino; 02-04-2008 at 06:03 AM. Reason: nevermind
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can help isolate the problem yourself by either stepping through the code in your debugger or by adding printf() statement to see where the problem lies.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    i am almost giving up on it
    i dont know whats the reason from this exersize
    if the library functions are there why not use them
    the only guess i have is that they want to split it for only the spaces
    ohhhhh i am getting a big headache

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Todd thanks for passing by,

    i am 0 level beginner
    i tried playing around with the debugger i guess i bugged the hell out of it yet i don't have a lot of knowledge about it to use it effectively.
    i thought my mistake was obvious but the problem was with me, cause the requirements dont sound that impossible.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You allocate a pointer for userInput, but you don't allocate any space. Where is fgets() going to put the data it needs? Answer - somewhere - but you don't know where!

    http://www.cppreference.com/stdio/fgets.html

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First of all, you are "randomly changing things", which will not work unless you have a good guessing answer generator, and yours seems to be out of calibration today.

    Change your "char *userinput" back to "char userinput[256]", and change the "fgets" to "fgets(userinput, sizeof userinput, stdin)".

    Finally, I don't think this does what you want:
    Code:
          if( firstturn=0)
    And this:
    Code:
          printf("'%s, ", userInput[i]);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tokenizing a string
    By chinesepirate in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2008, 11:32 AM
  2. strtok tokenizing on spaces as well as my delimiter
    By snowblind37 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2004, 12:39 AM
  3. Looking at strings
    By k10spades in forum C Programming
    Replies: 9
    Last Post: 11-19-2003, 09:11 PM
  4. String Tokenizing
    By irncty99 in forum C++ Programming
    Replies: 21
    Last Post: 05-08-2003, 07:47 AM