Superficially, your problem is the use of scanf
- it stops at the first space it encounters. Using fgets
is usually the answer - and that might have been that. There was another problem in that you did not terminate your string (which led to the interesting last character - quite by chance, I must add), and you did not check for overflow in either the input or output buffers. Most of these points were made in other answers.
However - the problem is quite a bit more interesting, and harder, because you are using "non ascii" characters in your input. I am guessing your robbers have Scandinavian roots. This means that you not only need to worry about whitespace - you need to be using "wide characters".
I took the liberty of rewriting your example in "wide" form - meaning that it uses
fgetws - the wide equivalent of fgets
wprintf - the wide equivalent of printf
wcschr - the wide equivalent of strchr
As well as "wide" strings like L"wide string"
, "wide" characters like L'å'
, and "wide" format specifier %ls
.
A stylistic problem with your code is that really long if
statement that consisted of a long series of OR-ed
comparisons with individual characters. You can do one of three things:
- format more clearly with carriage returns and line continuations:
if( inputText[ipTextNum] == L'a' || \
inputText[ipTextNum] == L'e' || \
inputText[ipTextNum] == L'i' || \
inputText[ipTextNum] == L'o' || \
inputText[ipTextNum] == L'u' || \
inputText[ipTextNum] == L'y' || \
inputText[ipTextNum] == L'å' || \
inputText[ipTextNum] == L'ä' || \
inputText[ipTextNum] == L'ö')
Note: you did not test for space, so space is turned into o
which is not what you want according to your description.
2. Replace the whole thing with wcschr
which looks for a character in a string; by searching for the character in the string of vowels (this is what I did in the code below - including test for space); or
3. You could also create a function for yourself like
int isVowel(wchar_t c) {
return wcschr("aeiouyåäö ", c)!=0;
}
To make it even more compact/readable.
Anyway - here is the complete, annotated code:
#include <stdio.h> // <<<< dont forget your header files
#include <wchar.h> // <<<< to be able to use 'wide' characters: ö etc
// define the max size of input and output string:
#define IP_MAX 100
#define OP_MAX 300
int main(void) { // <<<<< use the correct function signature
wchar_t inputText[IP_MAX], outputText[OP_MAX];
int inputLength, ipTextNum, opTextNum=0; // <<< initialize opTextNum to zero!!
// scanf("%s", &inputText); // <<<< triple NO:
// 1) scanf stops at the first space
// 2) if the input is very long you overwrite your buffer
// 3) you are passing the POINTER to the pointer to char;
// you should use inputText not &inputText
fgetws(inputText, IP_MAX, stdin); // read at most IP_MAX characters into inputText including '\0'
inputLength = wcslen(inputText); // length of wide C string
inputText[--inputLength]='\0'; // strip carriage return
// printf("The length of the string entered is %d\n", inputLength);
wprintf(L"you entered \n'%ls'\n", inputText);
for(ipTextNum = 0; ipTextNum < inputLength && opTextNum < OP_MAX-3; ipTextNum++) {
if(wcschr(L"aeiouyåäö ", inputText[ipTextNum])) { // <<< include test for space too
outputText[opTextNum] = inputText[ipTextNum];
opTextNum++;
}
else {
outputText[opTextNum] = inputText[ipTextNum];
outputText[opTextNum+1] = 'o';
outputText[opTextNum+2] = inputText[ipTextNum];
opTextNum += 3;
}
}
outputText[opTextNum]=L'\0'; // nul terminate the string
wprintf(L"The robber said:\n'%ls'\n\n", outputText);
return 0;
}
Output:
hello world
you entered
'hello world'
The robber said:
'hohelollolo wowororloldod'