Thread: Please help: counting string occurances using scanf and arrays

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Nancy Franklin's Avatar
    Join Date
    Sep 2011
    Posts
    15

    Please help: counting string occurances using scanf and arrays

    Goal: count the number of unique strings from the input stream. The typedef, STRING, scanf, and %s are required. After that, sort each unique string according to how many times its appeared. The output of the program should look like:

    word3 6 //word3 has appeared 6 times
    word1 3 //word1 has appeared 3 times
    word4 2 word4 has appeared 2 times
    word2 1 //word2 has appeared 1 time

    My approach: use two arrays: int count[] to store the number of occurances, and char* word[100], which stores each string only if it is unique. The variable counter is the link between the array indexes: when counter=4, count[4] is the number of times that the string stored in word[4] has occured. With each iteration of the while loop, a different word from the input is processed.

    My problem #1: The incoming word is stored in EVERY single index of count[] before counter. It should only be stored in word[counter]. Instead, the end of output looks like:

    s not found.
    counter is: 4
    word in array: dogvvv
    it was entered into: word[4]
    printing count/word arrays:
    count[j] is: 1
    word[j] is: dogvvv
    count[j] is: 1
    word[j] is: dogvvv
    count[j] is: 1
    word[j] is: dogvvv
    count[j] is: 1

    I'm getting that just from running this simplified version of it:

    Code:
    #include <stdlib.h>
     #include <stdio.h>
     #include <string.h>
    
     typedef char STRING[20];
     int main(int argc, char *argv[]){
           STRING s;
           int counter=0;
           char* word[100];
           int count[100];
           int place=0;
           int fakebool=1; //acts like a java Boolean variable
           int i;      int j;      int k;
           for(i=0; i<101;i++){count[i]=0;}    //fills count[] with 0s
           for(i=0; i<101;i++){word[i]=" .";}  //fills word[] with “ .”
                 while(scanf("%s",s)==1){
                 printf("current word: %s\n",s);
                 word[place]=s;
                 printf("s added.\n");  
                 printf("counter is: %i \n", counter);
                 printf("word in array: %s\n", word[counter]);
                 printf("it was entered into: word[%i]\n",counter);
                 count[counter]=1; //because its the first occurrence
                 printf("count/word arrays: \n");
                
                 for(j=0;j<5;j++){
                             printf("count[j] is: %i",count[j]);
                             printf(" word[j] is: %s\n",word[j]);
                 }
                 counter++;
                 printf("counter is now: %i \n", counter);
                 place++;
                 printf("w: \n");
           } //end if not there
     }//end while
           //exit(0);
    
     //}
    Trouble#2: In the longer version of this, I'm having trouble comparing strings, so the program knows whether it should add the incoming string to the end of word[] because it is unique. (If the incoming string is a duplicate of something already entered, it should just increase its value in count[].) But this never recognizes a duplicate word!
    Code:
    #include <stdlib.h>
     #include <stdio.h>
     #include <string.h>
    
     typedef char STRING[20];
     int main(int argc, char *argv[]){
           STRING s;
           int counter=0;
           char* word[100];
           int count[100];
           int place=0;
           int fakebool=1; //acts like a java Boolean variable
           int i;      int j;      int k;
           for(i=0; i<101;i++){count[i]=0;}    //fills count[] with 0s
           for(i=0; i<101;i++){word[i]=" .";}  //fills word[] with “ .”
                 while(scanf("%s",s)==1){
                 printf("current word: %s\n",s);
    for(i=0; i<counter;i++){ //check if s is already there k=strcmp(s,word[i]); //compare the strings if(k<0){ // s is already in array fakebool=0; printf("*******s found.********\n"); count[i]++; printf("numOccurances: %i \n", count[i]); printf("the count/word arrays: \n"); for(j=0;j<5;j++){ printf("count[j] is: %i",count[j]); printf(" word[j] is: %s\n",word[j]); } printf("ended found printer \n"); counter++; //it has been added } //end if there //printf("ended if found \n"); }//end check for if(fakebool==1){ printf("s not found.\n");
    word[counter]=s; printf("s added.\n"); printf("counter is: %i \n", counter); printf("word in array: %s\n", word[counter]); printf("it was entered into: word[%i]\n",counter); count[counter]=1; //because its the first occurrence printf("count/word arrays: \n"); for(j=0;j<5;j++){ //printing the arrays printf("count[j] is: %i",count[j]); printf(" word[j] is: %s\n",word[j]); } }// end the if()fakebool=1 statement counter++; printf("counter is now: %i \n", counter); printf("w: \n"); } //end if not there }//end while //exit(0); //}
    Perhaps using a 2-dimensional array would help, but I haven't used those in C yet, but if it makes it simpler, that's good...
    Last edited by Nancy Franklin; 09-22-2011 at 11:58 PM. Reason: adding more

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting Occurances of a Character
    By eeengenious in forum C Programming
    Replies: 5
    Last Post: 03-31-2011, 07:50 AM
  2. Replies: 18
    Last Post: 09-28-2010, 11:07 AM
  3. Counting String Occurances
    By Lesaras in forum C++ Programming
    Replies: 5
    Last Post: 12-14-2006, 09:43 AM
  4. Need help fast with counting consecutive occurances
    By c_323_h in forum C++ Programming
    Replies: 1
    Last Post: 07-04-2006, 12:48 AM
  5. occurances of string in a file
    By MB1 in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2005, 04:03 PM

Tags for this Thread