Thread: String - word counting!

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007

    String - word counting!

            char n1[50];
    	char n2[50];
    	int a=0,br=0,x;
    	char *p1,*p2;
    	puts("word: ");
    	puts("sentence: ");
    	while(*p2 != '\0')
    		if(*p1 == *p2)
    	printf("%d times word is shown, lenght of a word is %d",  a, x);
    What I need is to show how many times the word is shown for itself and how many times like a part of a sentence. I have some idea of comparing 'word' with lenght of words in the sentence and then to use counter but i cant get it done. My brain is blocked!

  2. #2
    Registered User
    Join Date
    Sep 2006
    You are trying to do something by comparing *p1 with *p2. When is the only time what these pointer's point to, would be equal?

    Only when the word entered, also was the sentence entered. So that's not what you want at all.

    C has some great C string functions that will help you greatly to do what you want. You need to include <string.h>, and then use a string function that looks through another string, searching for an instance of a word (or substring), you give it.

    Each of these string functions has a return value to indicate if it found something or did something, etc. That returned value, will serve as your while loop controller. In pseudo code:

    While (there are more instances of this word in this string)
    end of while

    So, hunt down the string function that does what I'm talking about, and put it to work!

    Have Fun!
    Last edited by Adak; 07-11-2007 at 04:39 AM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Also, using gets() is a bad idea.

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM