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

void processString(char *str, int *totVowels, int *totDigits);

int main()
{
    char str[50], *p;
    int totVowels, totDigits;

    printf("Enter the string: \n");
    fgets(str, 80, stdin);

    if (p = strchr(str, '\n')) *p = '\0';

    processString(str, &totVowels, &totDigits);
    printf("Total vowels = %d\n", totVowels);
    printf("Total digits = %d\n", totDigits);

    return 0;
}

void processString(char *str, int *totVowels, int *totDigits)
{
    int i, j;
    char tester[11] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};

    *totDigits = 0;
    *totVowels = 0;

    for (i = 0; i < strlen(str); i++)
    {
        if (isdigit(str[i]))
        {
            *totDigits += 1;
        }
        else if(isalpha(str[i]))
        {
            for (j = 0; j < 11; j++)
            {
                if (strcmp(str[i], tester[j]) == 0)
                {
                    *totVowels+=1;
                }
            }
        }
    }
}

My code is trying to calculate the number of times a number and vowel appeared in a string.

My string compare is trying to check for vowels but the program ends when it reaches the strcmp line. Why is that happening? Is my syntax wrong?

P.S. I'm only allowed to edit in the processString function, the rest are all given.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • 4
    If you don't get a compilation error for `strcmp(str[i],tester[j])` then fix your compiler settings until you do get an error. It is a complete waste of time trying to run a program that has errors – M.M Nov 08 '21 at 03:06
  • 1
    To be a bit more explicit, `str[i]` and `tester[j]` are both a single `char`. Whereas `strcmp` requires them to be both `char *` (ie C strings). The compiler should be giving you warnings about that which you should not be ignoring. To check a single character do `if (str[i] == tester[j])` – kaylum Nov 08 '21 at 03:08
  • [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) – Andreas Wenzel Nov 08 '21 at 03:11
  • 1
    `str` can hold 50 characters and you tell `fgets` to read up to 80 characters into it? – Nate Eldredge Nov 08 '21 at 03:14
  • @NateEldredge it is given by the site, i can't edit that. – Tajoh Programmer Nov 08 '21 at 03:19
  • 2
    Ugh. Whatever "the site" is, you might want to stop using it. – Nate Eldredge Nov 08 '21 at 03:22
  • @NateEldredge And please let the rest of us know what site it is so we can avoid it. – Keith Thompson Nov 08 '21 at 03:26

1 Answers1

0

Your problems lies is the code if (strcmp(str[i], tester[j]) == 0). Because you are referencing single-dimensional arrays, the dereference comes out to be a single character. Essentially you are comparing two chars, and chars can be compared for equality like ints. And strcmp is designed for comparing strings, not single chars. That is why it segfaults (segfaults are due to invalid pointer dereferences. In this case it tried to dereference a non-pointer. A definite no-no.)

The fix would be to replace that line with: if (str[i] == tester[j]).

ADBeveridge
  • 650
  • 3
  • 15