I'm facing an issue while trying to resolve a particular problem in C. User input must be read in encrypted form and the output must be the decrypted message, that is, it is a replacement cipher.The problem is that when I read the first input, the program loops printing the decrypted message, it doesn't ask to read other inputs.
I'm really confused about this as I was told that I should use EOF, something I'm not very knowledgeable about. I could even solve it another way, but I need to implement the EOF, and because I don't know that, I'm having difficulty solving the exercise.
I am aware that the code I created is not the best for the resolution, however, it might work if someone helps me with this loop issue.
Source Code
#include <stdio.h>
#include <string.h>
void alterChar(int i, char phrase[])
{
for (i = 0; phrase[i] != '\0'; i )
{
if (phrase[i] == '@')
{
phrase[i] = 'a';
}
if (phrase[i] == '&')
{
phrase[i] = 'e';
}
if (phrase[i] == '!')
{
phrase[i] = 'i';
}
if (phrase[i] == '*')
{
phrase[i] = 'o';
}
if (phrase[i] == '#')
{
phrase[i] = 'u';
}
printf("%s\n", phrase);
}
}
int main()
{
int i;
char phrase[256];
while (scanf("%[^\n]s", phrase) != EOF)
{
///scanf(" %[^\n]s", phrase);
alterChar(i, phrase);
}
return 0;
}
CodePudding user response:
i think just putting the printf outside of the loop help and use feof
(fetch en of file) to know where you are.
#include <stdio.h>
#include <string.h>
void alterChar(char phrase[]) {
for (int i = 0; phrase[i] != '\0'; i ) {
if (phrase[i] == '@') {
phrase[i] = 'a';
}
if (phrase[i] == '&') {
phrase[i] = 'e';
}
if (phrase[i] == '!') {
phrase[i] = 'i';
}
if (phrase[i] == '*') {
phrase[i] = 'o';
}
if (phrase[i] == '#') {
phrase[i] = 'u';
}
}
printf("%s\n", phrase);
}
int main() {
char phrase[256];
while (!feof(stdin)) {
scanf(" %[^\n]s", phrase);
alterChar(phrase);
}
return 0;
}