does char myStr[varLength]
work in C?
I have the following (code here):
int isVMin(char c){
return c == 'a' ||
c == 'e' ||
c == 'i' ||
c == 'o' ||
c == 'u' ||
c == 'y';
}
int isNum(char c){
return c>='0' && c<='9';
}
int removeChars(char str[]){ // removes all vowels, returns the digits number
int slen = strlen(str);
//char* res = malloc(sizeof(char)*slen+1);
char res[slen + 1]; /// <<<<<<<<<<<<<<<<<<<<<<<<<<<<< DOES IT WORK AS EXPECTED???
printf("\nthe initial is: '%s'\n", res);
int numCount = 0, j = 0;
for (int i = 0; i < slen; i++) {
if (isVMin(str[i]));
//str[i]= ' ';
else {
res[j++] = str[i];
if (isNum(str[i]))
numCount++;
}
}
res[j] = '\0';
printf("\nthe removed is: '%s'\n", res);
//str = res;
return numCount;
}
int main(){
char phrase[50];
gets(phrase);
int nb = removeChars(phrase);
printf("\nLa phrase '%s' contient %d digits", phrase, nb);
return 0;
}
The program compiles and work as expected. However I have doubts if this usage is legal in C...
char res[slen + 1]; /// <<<<<<<<<<<<<<<<<<<<<<<<<<<<< DOES IT WORK AS EXPECTED???
This works starting in C '99.
You're kind of limited as to how big. A rule of thumb would be exceeding 8k is probably not recommended. Note that on Windows, if the whole stack exceeds 1mb, the program will crash. On modern Unix. the same thing happens at a larger limit.
You can't return it though. Just sayin'