Reputation: 55
I want to know how ro get input as is.
For example:
input: 1
ab c
I want that the output will be:
bc d
Code
char x;
int shift;
printf("Please enter shift number:");
scanf("%d",&shift);
printf("Please enter text to encrypt:\n");
while (scanf(" %c",&x)!=EOF)
{
if(x<='z' && x>='a')
{
x=(x-'a'+shift)%(NUM_LETTERS)+'a';
printf("%c",x);
continue;
}
if(x<='Z' && x>='A')
{
x=(x-'A'+shift)%(NUM_LETTERS)+'A';
printf("%c",x);
continue;
}
else{
printf("%c",x);
}
}
return 0;
}
Is there a possibility that the user will type letters while passing a line until he clicks on CTR-z?
Upvotes: 1
Views: 139
Reputation: 154280
Changes needed
Read a line for shift
In preparation for the later while()
loop, all of the shift
input line needs consumption.
scanf("%d",&shift);
// add 2 lines to consume the rest of the line of input
// such as trailing spaces and \n
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
Read, but do not skip white-space
The " "
in " %c"
consumes without saving leading white-space.
// while (scanf(" %c",&x)!=EOF)
while (scanf("%c",&x)!=EOF) // drop space from format
Style suggestion:
Rather than test for inequality of an undesired return value, test for equality of a desire return value.
// while (scanf("%c",&x) != EOF)
while (scanf("%c",&x) == 1)
Upvotes: 1
Reputation: 91
Actually yes, it's possible. But here is a trick which you should notice:
scanf
- returns number of actually read values. But you need to check whether the input was ended. For that use getchar
function, for example, in your main loop.
Upvotes: 2