0

The task I am trying to accomplish is take a string from the user and delete all of the vowels using pointers. The issue I am having is the code I currently have will not run and I am unsure why. I must use pointers for the assignment as well as bring delete the vowels in a different function. Thanks.

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

int main(void)
{
    char *i, *ptr;
    char string[50]
    void delvow(char *ptr);

    printf("Enter a sentence that is not more than 50 characters long: ");
    scanf(" %s", &string);
    ptr = string;
    delvow(ptr);

    printf("Your new sentence is: %s",string);
    }
    void delvow(char *ptr)
    {
    int i = 0, j, k;
    printf("Sentence in delvow is: ");
    int length = strlen(string);
    while(string[i] != '\0')
    {
        for(j=0, j<(length-i); j++)
        {
            if(string[i+j] == 'A' || string[i+j] == 'a' || string[i+j] == 'E' || string[i+j] == 'e' 
              || string[i+j] == 'I' || string[i+j] == 'i' || string[i+j] == 'O' || string[i+j] == 
              'o' ||string[i+j] == 'U' || string[i+j] == 'u')
            {
                k=1;
                continue;
            }
            else
            {
                break;
            }
        }
        for(k = 1; k < length; k++)
        {
            string[k]=string[k+j];
        }
        i++
    }
}
  • 1
    "will not run". Can you please clarify what that means exactly? The program is not entitled to refuse to run :-) So what actually happens when you run it? Please give the input, expected output and actual output/behaviour. – kaylum Dec 05 '19 at 04:49
  • Are you allowed to make a copy of the original string or you need to do it in place? – kaylum Dec 05 '19 at 04:52
  • the posted code does not compile! when run through a compiler (In this case `gcc`,) the result is several `error` messages and a final summation of : `Compilation failed.` – user3629249 Dec 06 '19 at 04:24
  • The posted code fails to check for the "and sometimes 'y'" is also considered a vowel. – user3629249 Dec 06 '19 at 04:26
  • When compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same results – user3629249 Dec 06 '19 at 04:28
  • OT: regarding: `scanf(" %s", &string);` 1) always check the returned value (not the parameter values) to assure the operation was successful. In this case, any value returned other than 1 indicates an error occurred. 2) when using the input format specifiers `%s` and/or `%[...]` always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer because those specifiers always append a NUL byte to the input. This also avoids any possibility of a buffer overflow and the attendant undefined behavior. Suggest: `if( scanf(" %49s", &string) != 1) { // handle error }` – user3629249 Dec 06 '19 at 04:33

2 Answers2

0

You code has lot of syntax errors that is why it was not compiling and the logic of the program is not correct either. The below code has all the syntax error's corrected and the corrections are given in the comments.

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

void delvow(char *ptr); // Function declaration moved here

int main(void)
{
    char *i, *ptr;
    char string[50]; // ; missing
    //void delvow(char *ptr); Function declaration is moved outside the main()

    printf("Enter a sentence that is not more than 50 characters long: ");
    scanf(" %s", &string);
    ptr = string;
    printf("The input:%s",string);
    delvow(ptr);

    printf("\nYour new sentence is: %s",string);
}
void delvow(char *string) // ptr --> string
{
    int i = 0, j, k;
    int length = strlen(string);
    while(string[i] != '\0')
    {
        printf("\n1. string[%d]:%c",i,string[i]);
        for(j=0; j<(length-i); j++) //Replaced ',' with ';'
        {
            printf("\n2. string[%d]:%c",i+j,string[i+j]);
            if(string[i+j] == 'A' || string[i+j] == 'a' || string[i+j] == 'E' || string[i+j] == 'e' 
              || string[i+j] == 'I' || string[i+j] == 'i' || string[i+j] == 'O' || string[i+j] == 
              'o' ||string[i+j] == 'U' || string[i+j] == 'u')
            {
                k=1;
                continue;
            }
            else
            {
                break;
            }
        }
        printf("\n3. Value of j:%d",j);
        for(k = 1; k < length; k++)
        {
            printf("\n4. Repacing character in string[%d]:%c with charcter at string[%d]:%c",k,string[k],k+j,string[k+j]);
            string[k]=string[k+j];
        }
        printf("\n5. String:%s",string);
        i++; // missing ';'
    }
}

Your delvow function is a little complex and there is much easier way's to find and replace the vowels in a string, so I thing it will be better for you to use a simple function like the one i have given below. If you want to debug and fix the logic of your original delvow function I have added some debug print messages in the above code which will help you in your cause.

void delvow(char *string) // ptr --> string
{
    char ch;
    unsigned int i;
    unsigned int string_index = 0;
    int str_len = strlen(string);
    for(i=0;i<str_len;i++)
    {
        ch = string[i];
        if( ch=='A' || ch=='a' || ch=='E' || ch=='e' || ch=='I' || ch=='i' || ch=='O' || ch=='o' || ch=='U' || ch=='u' )
        {
            continue;
        }
        else
        {
            string[string_index] = ch;
            string_index++;
        }
    }
    string[string_index] = 0; // Terminating the new string with null character
}

The scanf in your code can take sentaces as input(strings with spaces) see this post to get an idea about how to input sentences.

0

We can easily delete the vowels in-place without copying. Here you go.

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

int main(void)
{
    char *ptr;
    char string[50];
    void delvow(char *ptr);

    printf("Enter a sentence that is not more than 50 characters long: ");
    scanf("%[^\n]s", string);
    ptr = string;
    delvow(ptr);

    printf("Your new sentence is: %s",string);
}

void delvow(char *string)
{
    char *strmchr(char *str, const char *delim);
    char* delim = "AaEeIiOoUu";
    char* cp;
    // let's not copy the chars out, let's delete them in-place!
    while ((cp=strmchr(string, delim)) != NULL)
        memmove(cp,cp+1,strlen(cp+1)+1);
}

char *strmchr(char *str, const char *delim)
{
    int length_str = strlen(str);
    int length_delim = strlen(delim);
    char *cp;
    for(int ix=0; ix < length_delim; ix++)
        if ((cp=strchr(str,delim[ix])) != NULL)
            return cp;
    return NULL;
}

Enter a sentence that is not more than 50 characters long: now is the time for all good men

Your new sentence is: nw s th tm fr ll gd mn

Level 42
  • 400
  • 2
  • 7