1

I wrote the following code in C to delete the vowels in a sentence:

#include <stdio.h>
#include <conio.h>

int main()
{
    char c[200],c1[200];
    int i,j;
    printf("enter the string: ");
    gets(c);
    for(i=0,j=0;c[i]!='\0';i++)
    {
        if(c[i]=='a' || c[i]=='e' || c[i]=='i' || c[i]=='o' || c[i]=='u');
        else
        {
          c1[j++]=c[i];
        }
    }
    printf("string after removal of vowels: %s",c1);  
}

Normally, all types of input works, but if we use "a" two times as input, in the start once, and at the end of the string, it gives error:

Input: jjhakl jhga
Output: jjhkl jhg@
Expected Output: jjhkl jhg

It works okay if the total string length is less, try longer string to get the error.

user694733
  • 15,208
  • 2
  • 42
  • 68
  • 2
    `c1` is not zero terminated. Add `c1[j] = 0;` after the loop before printing or `char c1[200] = {0};` to initialize all of the values to zero. – Retired Ninja May 24 '23 at 08:10
  • [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) is worth a read. Code using it won't work on a lot of compilers because it has been removed. – Retired Ninja May 24 '23 at 08:11
  • 1
    In the first comment, @RetiredNinja really answered the question. I have closed it as a duplicate to a missing null terminator FAQ that might be worth a read as well. – Lundin May 24 '23 at 09:39

0 Answers0