String - word counting!

This is a discussion on String - word counting! within the C Programming forums, part of the General Programming Boards category; Code: char n1[50]; char n2[50]; int a=0,br=0,x; char *p1,*p2; p1=n1; p2=n2; puts("word: "); gets(p1); puts("sentence: "); gets(p2); x=strlen(p1); while(*p2 != ...

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    String - word counting!

    Code:
            char n1[50];
    	char n2[50];
    	int a=0,br=0,x;
    	char *p1,*p2;
    	p1=n1;
    	p2=n2;
    
    	puts("word: ");
    	gets(p1);
    	puts("sentence: ");
    	gets(p2);
    	
    	x=strlen(p1); 
    	while(*p2 != '\0')
    	{
    		if(*p1 == *p2)
    		{
    			a++;
    		}
    		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
    Posts
    8,868
    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)
    a++;
    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
    Location
    Canada
    Posts
    8,046
    Also, using gets() is a bad idea. http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    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. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03: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, 04: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

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