0

I have this code, what i want it to do is print the string that represents the word, and print the number of times it occurred in the file, instead it outprints something liek this: (a load of blank space) and then this number -1076720020, which i have no idea where it came from, how would i go about fixing this?

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


struct podatki {
    char beseda[1000];
    int frekvenca;
};

void zamenjaj(char *str1, char *str2) {
   char *beseda2 = (char *)malloc((strlen(str1) + 1) * sizeof(char));
   strcpy(beseda2, str1);
   strcpy(str1, str2);
   strcpy(str2, beseda2);
   free(beseda2);
} 


int posodobi(struct podatki s[], const char unit[], int count) {
    int i =0;

    for (i = 0; i < count; i++) {
        if (strcmp(s[i].beseda, unit) == 0) {
            s[i].frekvenca++;
            return count;
        }
    }
    strcpy(s[count].beseda, unit);
    s[count].frekvenca++;
    return (count + 1);
}




int main() {
    int stBes;
    scanf("%d", &stBes);
    //zacetne deklaracije
    struct podatki s[1000];
    char string[1000], unit[2000], c;
    int i = 0;
    int frekvenca = 0; 
    int j = 0; 
    int count = 0;
    int num = 0;
    //branje 
    for (i = 0; i < 1000; i++) {
       s[i].frekvenca = 0;
    }

    i = 0;
    do {
       fflush(stdin);
       c = getchar();
       string[i++] = c;
    } while (c != '\n');


   //pretvori v majhne crke
   char *p;
   for (p = string; *p != '\0'; ++p) {
        *p = tolower(*p);
   }

   string[i - 1] = '\0';

   for (i = 0; i < strlen(string); i++) {
        while (i < strlen(string) && string[i] != ' ' &&  !ispunct(string[i])) {
       unit[j++] = string[i++];
    }
    if (j != 0) {
        unit[j] = '\0';
        count = posodobi(s, unit, count);
        j = 0;
    }
}

int a;
for (i = 0; i < count; ++i) {
    for (j = i + 1; j < count; ++j) {
        if (s[i].frekvenca < s[j].frekvenca) {
            a =  s[i].frekvenca;
            s[i].frekvenca = s[j].frekvenca;
            s[j].frekvenca = a;
            zamenjaj(s[i].beseda, s[j].beseda);

        }
    }
}

for (i = 0; i < count; i++) {
    for (j = 1; j < count; j++) {
        if (s[i].frekvenca == s[j].frekvenca){
            if (strcmp(s[i].beseda, s[j].beseda) < 0) {
                a =  s[i].frekvenca;
                s[i].frekvenca = s[j].frekvenca;
                s[j].frekvenca = a;
                zamenjaj(s[i].beseda, s[j].beseda);
            }
        }
    }
}

//printanje
for (i = 0; i < stBes; i++) {
    printf("%s\t   %d\n", s[i].beseda, s[i].beseda);
    if (s[i].frekvenca > 1) {
        num++;
    }
}
return 0;

}

Muffin123
  • 41
  • 5
  • 2
    First problem `fflush(stdin);` -> undefined behavior. And this `while (i < strlen(string)` is a very bad thing to do, since `strlen()` computes the length of the string, so you are doing an operation that gives the same result multiple times. – Iharob Al Asimi Apr 17 '15 at 14:18
  • 1
    Your stack is over 1 million bytes large, on Windows the default stack size if one single megabyte so if you're on Windows you're dangerously close to the limit of the stack. – Some programmer dude Apr 17 '15 at 14:23
  • 1
    [Obligatory warning about casting the result of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Paul R Apr 17 '15 at 14:27
  • I also see you using the `beseda` array from the structure a lot, and it seems to me that there are cases you use the array before it's initialized. That will of course lead to undefined behavior. – Some programmer dude Apr 17 '15 at 14:30
  • 2
    And to *really* find the source of your problem, you should really run the program in a debugger, and step through it line by line to see that it does what you expect it to do. – Some programmer dude Apr 17 '15 at 14:30

1 Answers1

1

The problem is that you convert the string to lower case before nul terminating it.

Here

i = 0;
do {
   fflush(stdin);
   c = getchar();
   string[i++] = c;
} while (c != '\n');
/* Goes here            <---------------------+ */
                      /*                      | */
//pretvori v majhne crke                      | */
char *p;              /*                      | */
for (p = string; *p != '\0'; ++p) {/*         | */
     *p = tolower(*p);/*                      | */
}                     /*                      | */
                      /*                      | */
string[i - 1] = '\0'; /* ---------------------+ */

You should also remove the fflush(stdin) and instead use getchar() to fetch the white space characters ignored by the previous scanf(), and please use scanf() correctly and check it's returned value.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Because you are overflowing the stack as @JoachimPileborg said, why are your buffers so large? did you think that memory had no limits? – Iharob Al Asimi Apr 17 '15 at 14:31