#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char array[1000];
int x, y, len = 0;
scanf("%s", array);
len = strlen(array);
for (x = 0; x < len; x ) {
if (array[x] == 'a' || array[x] == 'e' || array[x] == 'i' || array[x] == 'o' || array[x] == 'u' || array[x] == 'A' || array[x] == 'E' || array[x] == 'I' || array[x] == 'O' || array[x] == 'U') {
for (y = x; y < len; y ) {
array[y] = array[y 1]; // Moving the non-vowels to a higher position to fill up the array.
}
x--; // Deleting those particular x's (The vowels in place).
len--; // Deleting the length list.
}
array[len 1] = '\0';
}
printf("%s", array);
return 0;
}
The program is working fine until it uses an input that contains a space in the string, what can I do?
For example, "Hello World" prints "Hll" instead of "Hll Wrld".
CodePudding user response:
The scanf("%s", array);
takes input till white space character.
To take input till newline char can use scanf("%[^\n]", array);
And proper indentation is useful to understand code.
Just after this, the edited code works fine
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char array[1000];
int x, y, len = 0;
scanf("%[^\n]", array);
len = strlen(array);
for (x = 0; x < len; x ) {
if (array[x] == 'a' || array[x] == 'e' || array[x] == 'i' || array[x] == 'o' || array[x] == 'u' || array[x] == 'A' || array[x] == 'E' || array[x] == 'I' || array[x] == 'O' || array[x] == 'U') {
for (y = x; y < len; y ) {
array[y] = array[y 1]; // Moving the non-vowels to a higher position to fill up the array.
}
x--; // Deleting those particular x's (The vowels in place).
len--; // Deleting the length list.
}
array[len 1] = '\0';
}
printf("%s", array);
return 0;
}
CodePudding user response:
The problem is that by default scanf
accepts a space character as an input terminator, you can try using another function like fgets
Or set the conditions for the function using scanf("%[\n]", array);
CodePudding user response:
For starters the code is bad formatted.
And as any bad formatted code it has a bug.
This statement
array[len 1] = '\0';
can write outside the character array if the contained string does not contain a vowel.
For example consider the array
char array[] = "H';
The length of stored string is equal to 1. So this statement
array[len 1] = '\0';
is equivalent to
array[2] = '\0';
while the valid range of indices for this array is [0, 2)
.
The statement is just redundant and shall be removed because in this for loop
for (y = x; y < len; y ) {
array[y] = array[y 1]; // Moving the non-vowels to a higher position to fill up the array.
}
the terminating zero character '\0'
is moved to left as it is required.
Also instead of this long if statement
if (array[x] == 'a' || array[x] == 'e' || array[x] == 'i' || array[x] == 'o' || array[x] == 'u' || array[x] == 'A' || array[x] == 'E' || array[x] == 'I' || array[x] == 'O' || array[x] == 'U') {
you could use the standard function strchr
as for example
unsigned char c = array[x];
c = tolower( c );
uf ( strchr( "aeiou", c ) != NULL ) {
//...
Pay attention to that it is an inefficient approach to move a while sub-string to the left when a vowel is encountered.
As for you question then the conversion specifier s
"matches a sequence of non-white-space characters.". That is as soon as a white space character after a sequence of non-white space characters is encountered that input is interrupted.
You should write
scanf( "