C function trouble

hi everyone,
im writing a program to remove vowels from a string in c (not c++)
this is what i have got do far but im getting some errors.
char klinkers(char *s)
{
size_t len = strlen(s);
for(i=0; i < len;i++){
switch (data[i]) {
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’:
res = 1;
default:
res = 0;
}
if (res == 0) {
data[i] = prostr[j];
j++;
}
}
return (prostr);
}
what can i improve on this? any help would be really appriciated

Yes, lots of errors. I am not C expert, but I can see:

  • You need to break; in each case of you select statement, otherwise res will always be 0.
  • You haven’t defined a bunch of vars, such as i, res, data, prostr.

It seems you are trying to iterate through s, and copy all chars except the vowels from prostr to data.

What you are probably doing is just returning prostr, if it is defined anywhere, unchanged. The select case statement will always set res=0, because there are no break; lines in each case. You are trying to check each char in data, and then copy over it an undefined char from prostr…

I would google a c example of this, and see how they did it.

i forgot to say that, they are all defined as global variables.

Ok, fair enough. Fix the switch syntax and things might work a bit better. Then you can start optimising it a bit if you want.

switch (data[i]) {
	case ‘a’:
	case ‘A’:
	case ‘e’:
	case ‘E’:
	case ‘i’:
	case ‘I’:
	case ‘o’:
	case ‘O’:
	case ‘u’:
	case ‘U’:
		res = 1;
		break;
	default:
		res = 0;
	}

Maybe that will work.

The only way to learn programming is to find your mistakes. Use a debugger to step through your code and see what’s wrong. Only then does it make sense to look at the solutions of others.

Here’s mine:

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

void removeVowels(char *str) {
    char *i = str;

    for (char curc=*str; curc!='\0'; curc=*(++str)) {
        switch (curc) {
            case 'a':
            case 'A':
            case 'e':
            case 'E':
            case 'i':
            case 'I':
            case 'o':
            case 'O':
            case 'u':
            case 'U':
                break;

            default:
                *i = curc;
                ++i;
                break;
      }
    }

  *i = '\0';
}

void test(const char *str) {
    size_t len = strlen(str) + 1;
    char *strCopy = (char*)malloc(len);
    memcpy(strCopy, str, len);
    removeVowels(strCopy);
    
    printf("%s -> %s\n", str, strCopy);
    
    free(strCopy);
}

void main() {
    test("Hello, World!");
}

Can you allocate and clear a 256 element long array of bools/ints/whatevers and set the elements that need to be omitted to 1 , and then replace that switch statement with an array lookup.