1

I am trying to write a program to tell me how many 'e' letters are in the phrase "Yesterday, all my troubles seemed so far away". I am trying to do this using strcmp function and I want to compare each letter in the phrase to the letter 'e' and then make it so that the count increments.

int main(void) {
  int i, x, count = 0;
  char words[] = "Yesterday, all my troubles seemed so far away", letter;
  char str1 = "e";
  for (i = 0; i < 45; i++) {
    letter = words[i];
    x = strcmp(letter, str1);
    if (x == 0) {
      count++;
    }
  }
  printf("The number of times %c appears is %d.\n", str1, count);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
adamlsmith981
  • 63
  • 1
  • 8
  • 3
    You overcomplicate it just do `if (words[i] == 'e') count++;` in the loop. – Eraklon Mar 29 '20 at 11:23
  • 1
    `strcmp` requires a string. `letter` is a single character and not a stirng. A string in C is a sequence of characters terminated by a NUL. Use `==` to compare a single character. – kaylum Mar 29 '20 at 11:24
  • So I would only really want to use strcmp if lets say I wanted to find out how many time the word "cat" would appear in a sentance, for example? – adamlsmith981 Mar 29 '20 at 11:36
  • @asmit98 But that's a substring – Ardent Coder Mar 29 '20 at 11:37

2 Answers2

2

You can simply compare your string characters against the character 'e' like so

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

int main(void)
{
    int i, count = 0;
    char words[] ="Yesterday, all my troubles seemed so far away";

    for (i = 0; i < strlen(words); i++){
        if (words[i] == 'e') {
            count++;
        }
    }
    printf("The number of times %c appears is %d.\n", 'e', count);
}
Eraklon
  • 4,206
  • 2
  • 13
  • 29
2

For starters to use the function strcmp to count the number of appearances of a character in a string is a bad idea. Instead you should use the function strchr.

This declaration

char str1 = "e";

is invalid. In this declaration a pointer to the first character of the string literal "e" is converted to an object of the type char that does not make sense.

The call of strcmp

x = strcmp(letter, str1);

expects two arguments of the type const char * while in this statement there are passed two objects of the type char. So the call invokes undefined behavior.

You could write a separate function the following way as it is shown in the demonstrative program below.

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

size_t letter_count( const char *s, char c )
{
    size_t n = 0;

    if ( c != '\0' )
    {
        for ( const char *p = s; ( p = strchr( p, c ) ) != NULL; ++p )
        {
            ++n;
        }
    }

    return n;
}

int main(void) 
{
    char words[] = "Yesterday, all my troubles seemed so far away";

    char c = 'e';

    printf( "The number of times %c appears is %zu.\n", 
            c, letter_count( words, c ) );

    return 0;
}

The program output is

The number of times e appears is 6.
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335