0

I want to write a program that takes as an input a string and returns an encoded string. An encoded string would be string where each of the characters present in the original string are shifted by a few fixed number of places. Eg. Suppose each character in a string is shifted by 2 places. So if the user enters 'abcd' the output will be 'cdef'.

I am able to do it by taking the whole string as input and then processing each character to give the final output string.

But what I want is that as soon as the user enters a character, it should be shown in its encoded form and not its original form.

I tried the following code for a single character.

 char r;
    scanf("%c",&r);
    printf("\b%c",r+2);

But the user is still able to see he character he entered. The encoded character doesn't appear unless I press 'Enter'. How can I rectify this? Maybe using another function to get the character? Any help appreciated.

fts
  • 141
  • 4
  • 14

3 Answers3

2

If you are on UNIX, you can change the mode of the terminal like this:

#include <stdlib.h>
#include <stdio.h>

int main( void ) {
    system( "stty -icanon 2>/dev/null" ); // this line sets the mode

    while( 1 ) {
        int c = getc( stdin ); // a simpler way to get character input
        printf( "\b%c", c + 2 ); // print the shifted value
    }
}

stty -icanon sets "canonical input", which means you can get input the moment it is typed, instead of waiting for a newline (it's quite complicated and I don't fully understand everything it does, but it has the right effect). See the man page for full details. As I understand it, it sets a mode where a single character is enough to return the input, instead of waiting for a newline. You can also use min N to change that to wait for 2 or more characters, or time N to only wait a certain time before giving up (in tenths of a second).

The 2>/dev/null is a common syntax for redirecting stderr to /dev/null, which just means "ignore any errors".

On Windows, this won't work, and you should use getch as already suggested.

Finally, your cypher code is a bit odd; typing y and z will produce { and |!

Dave
  • 44,275
  • 12
  • 65
  • 105
0

If you are using MSVC, you can use getch(). #include

// You don't need type "Enter" key
char ch = getch();

If you are not using MSVC, you can refer to here: How to avoid press enter with any getchar()

Community
  • 1
  • 1
Eric Z
  • 14,327
  • 7
  • 45
  • 69
0

I compiled and run your code on my Borland Turbo C++(platform: Windows) and it is giving the output 'c' for the input 'a'. I don't where is your problem is arising or in which platform you're coding.

If you are looking for alternate function for character input in C, then few those are given as below:

getch():

Header: conio.h

Desc: Take one character as input from the console and return to the variable without echoing on the screen. It does not wait for 'enter' from the user for proceeding to the next line

Syntax: char ch = getch();

getche():

Header: conio.h

Desc: Serves same way as getch(), but the only difference is that it echo the entered character on screen.

Syntax: char ch = getche();

getchar():

Header: stdio.h

Desc: It is a macro of getc(stdin). For simplicity, it also takes an character as input from console and return to variable. It echos the character on screen and user have to press enter for proceeding to next operation.

Syntax: char ch = getchar();

I hope, it will help you.

try out the following code:

#include<stdio.h>
#include<conio.h>
void main()
{
    char ch = getch();
    printf("%c", ch);
}
joy-cs
  • 359
  • 3
  • 12
  • 1
    Yes. Even I am getting that. But what I actually want is that the user should not see the 'a' which he types. He should just see the encoded character which is 'c' in this case. – fts Aug 26 '13 at 17:15
  • Although I've already mentioned three functions out of which the first one may be required to you, i.e. getch(). I have modified the answer for you. Check once again. – joy-cs Aug 26 '13 at 17:31
  • @joy-cs `getch` is *windows only* and the OP is on UNIX. – Dave Aug 26 '13 at 17:54
  • @Dave Thanks for your information. I'm not practiced with Linux environment. So I already mentioned it that I'm on Windows platform. – joy-cs Aug 27 '13 at 03:06