-1

My program should first read an entered string word and then count the amount of repeated letters.

For example, if I enter apple it should print 1, but instead it prints 4.

I suppose word[i] = word[i + 1] isn't the right way to count?

#include<stdio.h>

int main() {

    int i, j, wordLength = 0, alphabeticalSort, counter = 0, swap;
    char word[51];

    scanf("%s", word);

    while (word[wordLength] != '\0'){
        wordLength++;
    }

    ...

    for (i = 0; i < wordLength; i++){
        if (word[i] = word[i + 1])
            counter++;
    }

    printf("\nNumber of repeated letters: %d", counter);

    ...

    return 0;
}
alcatraz
  • 41
  • 2
  • 6
  • 4
    `if (word[i] = word[i + 1])` should be `if (word[i] == word[i + 1])`. Also, consider including `string.h` and using `strlen()` instead of manually counting the length. These common functions exist for a reason. – cdhowie Nov 11 '18 at 21:20
  • Since it produces the wrong answer, what you've got clearly isn't what's needed. You need to count the number of occurrences of each letter (does 'Abracadabra' count 'A' and 'a' separately). Then you need to count the number of letters that have more than 1 occurrence. – Jonathan Leffler Nov 11 '18 at 21:21
  • Thanks guys. Also, is including `string.h` necessary when using `strlen`, `strcmp`, etc? I've used them in the past, but never included the `string.h` library. – alcatraz Nov 11 '18 at 21:22
  • If you're not coding in archaic C (pre-standard C, or C90), you need to include ``. You should include it even in C90, but it wasn't mandatory. Many compilers still use C90 as the default mode (GCC 4.x for example, but GCC 5.x and later — 8.2 is current — use C11 mode as the default). – Jonathan Leffler Nov 11 '18 at 21:23
  • 1
    Are you counting repeated adjacent letters, or repeated occurrences of a letter anywhere in the word (string)? It matters because 'abracadabra' has no adjacent repeats, but it repeats a, b, and r non-adjacently. So, it's count might be 0 or 3, depending on this detail in the specification. – Jonathan Leffler Nov 11 '18 at 21:25
  • Also cdhowie, I'm supposed to compare character with character (for example `apple` would give value 1) so would `strlen` even work in that case? Doesn't `strlen` compare the string lengths, and not character by character? – alcatraz Nov 11 '18 at 21:29
  • 1
    You really don't need the `wordLength` loop; you could simply modify your `for` loop to use: `for (int i = 0; word[i] != '\0'; i++)` to control it. If you must have `wordLength`, then use `wordLength = strlen(word);` instead of your loop. Yes, `strlen()` returns the length of a string. – Jonathan Leffler Nov 11 '18 at 21:32
  • That's a neat loop trick @JonathanLeffler, will def. use it in the future. – alcatraz Nov 11 '18 at 21:44

3 Answers3

1

It's best to split what you're doing into two functions, one of which will count the number of occurrences of each letter.

#define LETTERS 26

/* counts number of occurrences of each lowercase letter in str. Result is placed in outCounts, 
   assuming it has room */
void countletters(char *str, int outCounts[])
{
    memset(outCounts, 0, LETTERS * sizeof (int));

    for (; *str; ++str) {
        if (isupper(*str))
            *str = tolower(str);
        if (islower(*str))
            ++outCounts[*str - 'a'];
    }
}

Then you write another function will examines the outCounts array, which is modified. If a letter is repeated, the corresponding member of this array will be greater than one. I leave this as an exercise to the reader.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
0
int main()
{
    char string[80];
    int c = 0, count[26] = {0}, x;

    printf("Enter a string\n");
    //gets(string); 
    scanf("%s", string); 
    while (string[c] != '\0') {

       if (string[c] >= 'a' && string[c] <= 'z') {
          x = string[c] - 'a';
          count[x]++;
       }

       c++;
    }

    for (c = 0; c < 26; c++)
       printf("%c occurs %d times in the string.\n", c + 'a', count[c]);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sari Masri
  • 206
  • 1
  • 10
  • 5
    No! Never use `gets` – lost_in_the_source Nov 11 '18 at 21:24
  • 2
    See [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Nov 11 '18 at 21:27
  • 1
    You assume that all the characters in the string are lower-case alphabetic; I'm not convinced that's a safe assumption. I think you'd do better to ignore spaces and punctuation and digits (or non-alphabetic characters). Maybe map upper-case to lower-case and convert appropriately, or maybe count them separately; it isn't clear from the question which is expected. – Jonathan Leffler Nov 11 '18 at 21:29
  • Ah. `scanf`. [No better, is it?](https://stackoverflow.com/questions/2430303/disadvantages-of-scanf). – Bob Jarvis - Слава Україні Nov 11 '18 at 23:07
0

You have:

for (i = 0; i < wordLength; i++){
    if (word[i] = word[i + 1])
        counter++;
}

Look at the 'comparison' inside the loop; you're giving the value of word[i+1] to word[i+1]. This condition will always be true unless the word[i+1]'s value is a 0 or a null char which is also a 0 in the ASCII table.

Remember: the equality operator in C is always ==, so replace the = with ==.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mehdi Fracso
  • 448
  • 2
  • 16