Thread: help, i cant count spaces or words

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

    Unhappy help, i cant count spaces or words

    hey guys, i got a problem,

    im a big java fan and i am doing a c program for a project for school. The program states that you have to enter a string and then display the number of charecters, words and spaces in the string

    i have got it to successfully count the charecters, but when the words calculate, it displays how many spaces i allowed for the string

    the program is below, with tags to help you understand my program, its not urgent, but i just cant get it to display the words and spaces properly

    code is as follows:
    Code:
    Question 1 - Programming Assignment 2
    // String Reader with analysis
    // By Matthew Potter 0314377 22/11/05
    
    #include "stdio.h"
    #include "string.h"
    #include "conio.h"
    
    void main(void){ // start main	// declaring variables
    	
    
    char string[50];	
    char space = ' ';	
    int *totalspace; 	
    int length, words;	
    
    // taking user input	
    printf("\nEnter a sentence that is less than 50 letters\n") ;
    
    gets(string);		
    length=strlen(string); // lenghth of entered string
    totalspace=strchr(string, space);  // blank space calculator
    words=sizeof(string); // word count
    puts(string);	
    
    // Displaying Output To Screen 
    
    printf("\nThe String %s Is %d Charaters Long\n",string,length) ;
    printf("\nThere Are %d Words In Your String\n",words);
    printf("\nThere Are %d Spaces In Your String\n",totalspace);
    
    } // end main
    thanks guys, hope you can find my syntax error

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ok, few things. First main should be declared as and return an int. Second you should use fgets() instead of gets().

    As for your question, this is because you're using strchr() incorrectly. Infact, I don't know why you're using it at all, you can nice and easily knock out both words and spaces with a simple loop through your character array.
    Sent from my iPadŽ

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And you should (but don't have to) use angle brackets, not double quotes, for standard header files:
    Code:
    #include "stdio.h"
    ->
    Code:
    #include <stdio.h>
    Double-quotes are for your own header files (usually).

    This doesn't do what you think it does:
    Code:
    words=sizeof(string); // word count
    It will always be 50, the size of words[] in memory.

    BTW, Sly, your avatar's still down.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading a file of words into dynamic char**
    By strakerc in forum C Programming
    Replies: 11
    Last Post: 07-11-2008, 07:20 PM
  2. How to count the words in sentence ?
    By Th3-SeA in forum C Programming
    Replies: 1
    Last Post: 10-01-2003, 01:34 AM
  3. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM
  4. Checking number of words
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 07-11-2003, 02:13 PM
  5. words count
    By arlenagha in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2003, 09:29 AM