Thread: String Problem

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    1

    String Problem

    Ok, here's my problem:
    I am having a little trouble with string manipulation. The problem is that I am not sure if I am following the right procedure for my program. This is what I am suppose to do: 1) read in lines of text from the user 2) call a function to extract words one by one from the line 3) determine the length of each word 4) incrememnt an araay in order to store the size of each word 5) then call a function to compute weighted average and finally 6) return it to the main program to be printed. This what I have so far:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    main()
    {
    char input[81], temp[81], *word, *remain;
    printf("Enter sentences (or CTRL-z to terminate)=>");
    gets(input);
    while(strlen(input) > 0)
    {
    strcpy(temp, input);
    word = strtok(temp, " ");
    remain = strpbrk(input, " ");
    if(remain != NULL)
    strcpy(input, remain+1);
    else
    strcpy(input, " ");
    .......(confused....)
    If anyone could help me out with this, that would be awesome. I am really confused.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    use the code tags so people can help you, your program is not very readable the way you have posted it.
    When no one helps you out. Call google();

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Well you are not really going about this the right way. Lets see what we have so far:

    1) read in lines of text from the user

    Code:
    char input[81];
    
    printf("Enter sentences (or CTRL-z to terminate)=>");
    fgets(userInput,sizeof(userInput), stdin);
    This part is good so far with the exception of replacing gets with fgets.

    2)call a function to extract words one by one from the line

    And this is where everything goes all crazy. You do not have a function, which is the first thing I noticed. Secondly you seem like you do not have an understanding over how strtok() operates. strtok() replaces the delimeter with a null character and then returns a pointer to the first "word". Now subsequent calls to strtok() with NULL as the first argument will keep going through the same array passed to it. Now since I feel like being nice and I know strtok thows some people through loops I am including an example program to demonstrate its use. THIS DOES NOT ANSWER YOUR HOMEWORK ASSIGNMENT. It simply demonstrates strtok and loops.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    
        char userInput[81], *token[80]; //user input and array to hold max possible tokens, aka 80.
        int i=0; // used to iterate through array
    
        printf("Enter a line of text to be tokenized: ");
        fgets(userInput,sizeof(userInput), stdin);
    
        token[0] = strtok(userInput, " "); //get pointer to first token found and store in 0
                                           //place in array
        while(token[i]!= NULL) {   //ensure a pointer was found
            i++;
            token[i] = strtok(NULL, " "); //continue to tokenize the string
        }
        
        for(int j = 0; j <= i-1; j++) {
            printf("%s\n", token[j]); //print out all of the tokens
        }
    
        return 0;
    }
    Now once you've played around with this generate your function and post your updated code to get further guidance if you require it.
    Last edited by andyhunter; 01-31-2005 at 02:56 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Use fgets() instead of gets() to input strings for this reason..

    more on fgets() here
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Thanx for the catch.

    -Andy
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM