"How many word of the same kind in a sentence " program ..?

This is a discussion on "How many word of the same kind in a sentence " program ..? within the C Programming forums, part of the General Programming Boards category; Hello , This is a program intends to tell the number of the each word from the sentence. For example ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    151

    "How many word of the same kind in a sentence " program ..?

    Hello ,
    This is a program intends to tell the number of the each word from the sentence. For example If write

    naber aga naber naber aga

    output will be like this:

    naber 3
    aga 2

    I have written a program , I have a little problem I take a output like this :
    naber 3
    aga 2
    naber 3
    naber 3
    aga 2

    I know why I take this problem I have a solution . This is the codes by the way :
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void sonuc(char *,char *,int,int *);
    int main() {
    	char *s=malloc(40*sizeof(char));
    gets(s);
           char *b,c[40];
    	char *p[10];	
    	char a[40];
    	strcpy(a,s);
    	b=strtok(a," ");
    	int y=0;
    	int x;
    strcpy(p[0],b);
    	printf("%s",p[0]);
    	int i=1;
    	int m=0;
    	while(b!=NULL) {
           x=strlen(b);
    for(m=0;m<i;m++) {
    			if(strcmp(p[m],b)==0) {
                                  b=strtok(NULL," ");
    				continue; }
    		}
    			strcpy(c,s);
    		sonuc(c,b,x,&y);
                     printf("%s kelimesinden %d tane var\n",b,y);
    		y=0;
                b=strtok(NULL," ");
    		i++;
    		}
    	return 0;
    }
    
    
    
    
    
    		void sonuc(char *d,char *str,int t,int *ptr) {
    if(strstr(d,str)==NULL) return;
    else {
      d=strstr(d,str);
    			*ptr+=1;
    			sonuc(&d[t],str,t,ptr); }
    }
    algorithm is not really hard. I am sure you will figure it out easily.

    I have a solution for the problem . I can save each word into a struct or pointer string then I can compare the current word with the former words I have passed.If there is a same I can stop the loop to the beginning.

    I want to know if there is another solution or algorith for tis program which is far more simpler than mine.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    is my language hard to understand?

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    I want to know if there is another solution or algorith for tis program which is far more simpler than mine.
    yes, there is.

    but that's the point of learning how to develop software, is figuring out better ways. you want to be told the part we expect you to enjoy the most?! hah! never...

    ... well... i'm not taking the time to re-write your software to show how. up to you to discover the solutions. enjoy learning c++!

    actually, maybe you want to learn math/algorithms, that would be more ideal of a tool for this sort of question, as far as breaking things down intuitively go anyway.
    Last edited by simpleid; 08-15-2007 at 03:37 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    check the return value of malloc.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I'm quite amazed to see how some people are able to write such hard-to-read (and understand) code. I bet you had some compilation error/warning and semantic bug the first time you compiled/ran the source code. If not, duh, you are a real genious (truly). Those variable name makes so little sense, and what about the indentation! This is truly amazing, i don't think i could do something like this, men, it just look too complex.

    By the way this ain't C. And there's some "don't do this" in your code, like this

    Code:
    char *p[10];
    strcpy(p[0],b); // uh oh
    Anyway

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I am not genious for sure , it is just the way I do things . Ununderstandable ways. Anyway what is with that strcpy thing? Ofcourse you cant do p[0]? =) I really didnt get the point.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I am sorry or variables are turkish ,
    &#37;d kac tane var means
    "Number of ..... is %d".

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    I am not genious for sure , it is just the way I do things . Ununderstandable ways. Anyway what is with that strcpy thing? Ofcourse you cant do p[0]? =) I really didnt get the point.
    What is p[0] pointing at?

    --
    Mats

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    yea I am talking about that I didnt do such thing , I did not undertand why he mentioned that.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    He means that you can't just use char *p[10] like it's some magical place to shove text. Not only does the array have to have enough pointers but they should always point to some memory in order to be useful.

    Maybe an example will help.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *strdupl( const char orig[] ) {
      if ( orig ) {
        char *dup = malloc( strlen( orig ) + 1 );
        strcpy( dup, orig );
        return dup;
      }
      else {
        return NULL;
      }
    }
    
    int main( ) {
      size_t i, count;
      const char parseme[] = "C:/Documents and Settings/Owner/My Documents/";
      const char tok[] = ":/ ";
      char *parsable = strdupl( parseme );
      char *parts[8];
      char *seg;
    
      count = 0;
      printf( "Breaking apart \"%s\" by \"%s\":\n\n", parseme, tok );
      for ( seg = strtok( parsable, tok ); seg != NULL; seg = strtok( NULL, tok ) ) {
        parts[count] = seg;
        count++;
      }
      parts[count] = NULL;
    
      for ( i = 0; i < count; i++ ) {
        puts( parts[i] );
      }
      free( parsable );
      return 0;
    }
    Breaking apart "C:/Documents and Settings/Owner/My Documents/" by ":/ ":

    C
    Documents
    and
    Settings
    Owner
    My
    Documents

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ozumsafa View Post
    yea I am talking about that I didnt do such thing , I did not undertand why he mentioned that.
    It looks like this in the beginning of the code you posted:
    Code:
    gets(s);
           char *b,c[40];
    	char *p[10];	
    	char a[40];
    	strcpy(a,s);
    	b=strtok(a," ");
    	int y=0;
    	int x;
    strcpy(p[0],b);
    	printf("%s",p[0]);
    [I added the red]

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. word counting program
    By mashour06 in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 03:58 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. help me out Unrgent wit a cross word program
    By rags in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:12 AM
  4. Replies: 7
    Last Post: 06-16-2006, 09:23 PM

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