Home > Software engineering >  Issues with string in structure in C codig
Issues with string in structure in C codig

Time:01-08

I have some issues with strings in structure, i think that the problem is strcpy(), but i can't resolve it. My exercise says that i have to write a program that prints the distinct words given as input, sorted in order of frequency of decreasing occurrence and precede each word with its number of occurrences. I thought that scanf could do something wrong so i used fgets() to stdin to take my string from input, but it didnt resolve my problem :(. Here is my code (i haven't yet written the function that prints the words in order of frequency).

/*
Write a program that prints the distinct words given as input, sorted in order of frequency of
decreasing occurrence. Precede each word with its number of occurrences.
*/




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define SIZE 30
#define MAXWORD 5



typedef struct word_dictionary{

    char ww[SIZE];
    int occ;

}word;



int is_in_bucket(char *str, word data[]){

    int j;

    for(j=0;j<MAXWORD;j  ){

        if( strcmp(str,data[j].ww) == 0){
            
            return 1;
        }

    }


    return 0;



}




void print_bucket( word data[]){


    int i;
    
    for(i=0;i<MAXWORD;i  ){

        printf("{%d, %s} ", data[i].occ, data[i].ww);

    }

    printf("\n");


}




void main( int argc, char **argv)
{

    word bucket[MAXWORD];
    char aux[SIZE];
    int i;
    int x;
    int cond;

    for(i=0;i<MAXWORD;i  ){

        printf("Insert the word you want to save in the data structure: ");
        scanf("%s",aux);
        
        cond = is_in_bucket(aux,bucket);
        if(cond == 1){
            
            bucket[i].occ = bucket[i].occ   1;
            memset(aux,0,SIZE);

        }else{

            strcpy(bucket[i].ww, aux);
            bucket[i].occ = 1;
            memset(aux,0,SIZE);

        }
        
    }

    print_bucket(bucket);
    //print_occurrences();
    
    return;
    

}

My terminal output is:

Insert the word you want to save in the data structure: dog

Insert the word you want to save in the data structure: cat

Insert the word you want to save in the data structure: dog

Insert the word you want to save in the data structure: dog

Insert the word you want to save in the data structure: mouse

{1, dog} {1, cat} {-1768437999, } {1, V} {1, mouse}

My expected terminal output should be:

Insert the word you want to save in the data structure: dog

Insert the word you want to save in the data structure: cat

Insert the word you want to save in the data structure: dog

Insert the word you want to save in the data structure: dog

Insert the word you want to save in the data structure: mouse

{3, dog} {1, cat} {1, mouse}

CodePudding user response:

For starters the function main shall have the return type int

int main( int argc, char **argv)

As the function parameters are not used then it is better to declare the function like

int main( void )

The function is_in_bucket does not take into account the number of actual initialized elements in the array bucket. You need to pass this number to the function.

If a word is already present in the array it is not mean that it is stored exactly in the i-th element of the array.

    cond = is_in_bucket(aux,bucket);
    if(cond == 1){
        
        bucket[i].occ = bucket[i].occ   1;
        memset(aux,0,SIZE);

You need to return form the function is_in_bucket the index of already initialized element or for example -1 otherwise.

Also the function print_bucket shall take into account the actual number of filled elements of the array bucket.

  • Related