Ad
  • Default User Avatar

    I'm having a very strange issue with this one. My solution is mostly correct, except that it doesn't work for the letter "a". I've got a char array with each letter at it's alphabet position index - 1 (so 'a' has an index of 0) and when I check if a character from the input is in the alphabet array, the check for 'a' fails. It boils down to the statement

    // *text is 'a' on the iteration in question
    *text == alphabet_array[0]
    

    evaluating as false. I actually shifted the alphabet array by one element and put in a '?' as the first element and then it worked for the test input (but won't work generally). Characters that are in the first position of the alphabet array are failing for some reason, so my solution will skip whatever character is in the first position ('a' here).

  • Default User Avatar

    you cannot call strlen() on something like

    char vowels[] = {'a','e','i','o','u'};
    

    This is a char array, not a null-terminated string. strlen() works by iterating through the string until it finds a nul byte ('\0'). vowels does not contain one, so it will keep scanning the memory until a nul byte happens to be there by chance. this is undefined behavior and should be avoided at all costs. In the sample tests you are just being lucky.

    Also, performance note: you should almost never call strlen() in a loop on the same string: this makes your program slower for no reason; because unless the string's length is modified during the loop, strlen() will keep doing the exact same operation again and again and return the same result every time

  • Default User Avatar

    For a solution in C, for long input strings, I'm getting +3 of the expected answer consistently. I get the expected answer when compiling and running on my local machine.