C String Problem: Not reading end of string

This is a discussion on C String Problem: Not reading end of string within the C Programming forums, part of the General Programming Boards category; I have this program below that I have been working on, given for you advanced programmers this is probably cake, ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    Question C String Problem: Not reading end of string

    I have this program below that I have been working on, given for you advanced programmers this is probably cake, but my whole CS 1 class has struggled with it. I have it working except that unless I include a space after the last word of the string, then it won't register the last word. In the comments I have the whole problem that we got asked and have a pretty poorly mad algorithm, but the program is what I'm worried about. Please suggest what you can and please remember its a CSCI 261 class (Computer Science 1) and this is my first programming class. Thanks for your help in advance!


    Code:
    /*
    Write a program that processes a sequence of lines, displaying a count of the total number of words in those lines as well as counts of the number of words with one letter, two letters, and so on.
    
    Have the user input the sentences.
    Enter a for statement that checks for spaces
    When the for statement finds a space it will break the word up from the rest of the sentence
    I have predetermined there will be 5 words being less than or equal to five characters long
    It will then enter a if statement to calculate how many words have how many letters done by adding 1 to the number of letters (ex one=one+1)
    Return to the user how many words there are and how many words have each number of letters.
    Stop.
    */
    
    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>
    #define stringlength 50
    
    char string1[stringlength];
    char string2[stringlength];
    char string3[stringlength];
    char string4[stringlength];
    char string5[stringlength];
    int one=0, two=0, three=0, four=0, five=0;
    int i, index, word=0; 
    
    char count(char *);
    
    int
    main(void)
    {
    	printf("Please enter sentences less than 50 letters with words being no longer than 5 letters.\nPlease use no punctuation and to go on to the next sentence hit enter\n");
    	gets(string1);
    	gets(string2);
    	gets(string3);
    	gets(string4);
    	gets(string5);
    	count(string1);
    	count(string2);
    	count(string3);
    	count(string4);
    	count(string5);
    	printf("The string consists of:\nWords:  %d\n1 Letter words:  %d\n2 Letter words:  %d\n3 Letter words:  %d\n4 Letter words:  %d\n5 Letter words:  %d\n", word, one, two, three, four, five);
    	return(0);
    }
    
    char count (char string[])
    {
    	char word1[5];
    	int index;
    	for(i=0; i < strlen(string); i=i+1)
    	{
    		if(string[i]==' ' || string[i]=='\n' || string[i]=='\0')
    		{
    			word=word+1;
    			index=i;
    			strncpy(word1, string, index);
    			if(index==1)
    				one = one + 1;
    			else if(index==2)
    				two = two + 1;
    			else if(index==3)
    				three = three + 1;
    			else if(index==4)
    				four = four + 1;
    			else if(index==5)
    				five = five + 1;
    			strcpy(string, &string[index+1]);
    			i=0;
    		}
    	}
    	return(one, two, three, four, five, word);
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    1. Don't use gets(). See the FAQ
    2.
    Code:
    return(one, two, three, four, five, word);
    Doesn't do what you think it does.

    Why not just split the string up by spaces, and then count the letters in each "word"?

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Since ints one, two, three, four & five are all global variables, your count() function might as well return void.

    Unless you understand the concept of "scope" (and I ain't talkin' mouthwash), you ought to not use the variable "index" as a global variable and a local variable at the same time. Your current implementation won't bite you, but it could in the future if you don't watch your variable declarations closer.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    I have not run your code, but I think you will never meet the last word in the string as the function gets discards the new line character.
    use
    Code:
    if(string[i]==' ' ||  i=strlen(string)-1)
    instead

    Don't test the '\0', it's meaningless.

    By the way, don't use gets anymore since it's unsafe and has been deprecated. use fgets or getline instead. fgets and getline read the newline character into the buffer, so you can use
    Code:
    if(string[i]==' ' || string[i]=='\n')

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by bbebfe View Post
    I have not run your code, but I think you will never meet the last word in the string as the function gets discards the new line character.
    use
    Code:
    if(string[i]==' ' ||  i=strlen(string)-1)
    instead

    Don't test the '\0', it's meaningless.

    By the way, don't use gets anymore since it's unsafe and has been deprecated. use fgets or getline instead. fgets and getline read the newline character into the buffer, so you can use
    Code:
    if(string[i]==' ' || string[i]=='\n')
    Why test for that!?

    strtok() FTW.
    Last edited by zacs7; 11-17-2008 at 09:46 PM. Reason: Ops

  6. #6
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    Quote Originally Posted by zacs7 View Post
    Why test for that!?

    strtok() FTW.
    Your answer is better, strtok is a better choice. I'm also studying C programming from you all guys in this forum. thx.

    Code:
     i=strlen(string)-1)
    I want to test if the last character of the string encountered.
    Last edited by bbebfe; 11-17-2008 at 10:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 02:30 PM
  3. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21