1

I'm writing a program to 'encrypt' an inputted string of text by using a switch statement to correlate the given character with a symbol, and output that symbol in the place of the character. I put it in a while loop, the idea being that it would loop the full switch function each time until the received character is EOF. On a guess, I believe it is looping through just the first character, because I don't advance the getchar() statement, but I'm not sure how to do that so any help would be greatly appreciated. I say this because if I use return instead of break, it closes the while loop and only takes that first letter, if I use a break then it spams the first 'encrypted' char.

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

/* C program to encrypt a given text message, assuming all lowercase */

int main() {

    int Input, Encrypted;
    printf("Please type your message\n");

    Input = getchar();

    while (Input != EOF) {
        switch (Input) {
        case 'a':printf("!"); break;
        case 'b':printf("@"); break;
        case 'c':printf("#"); break;
        case 'd':printf("$"); break;
        case 'e':printf("%"); break;
        case 'f':printf("^"); break;
        case 'g':printf("&"); break;
        case 'h':printf("*"); break;
        case 'i':printf("`"); break;
        case 'j':printf("~"); break;
        case 'k':printf("-"); break;
        case 'l':printf("_"); break;
        case 'm':printf("="); break;
        case 'n':printf("+"); break;
        case 'o':printf("["); break;
        case 'p':printf("{"); break;
        case 'q':printf("]"); break;
        case 'r':printf("}"); break;
        case 's':printf(";"); break;
        case 't':printf(":"); break;
        case 'u':printf("|"); break;
        case 'v':printf(","); break;
        case 'w':printf("<"); break;
        case 'x':printf("."); break;
        case 'y':printf(">"); break;
        case 'z':printf("'");break;
        return 0;
        }
    }
    return 0;

}
  • Use ``getline()`` or fgets() instead of ``getchar()`` and loop through it. https://stackoverflow.com/questions/314401/how-to-read-a-line-from-the-console-in-c – defaultUsernameN Apr 03 '21 at 21:26
  • `"until the received character is EOF"` -- Are you sure you want to expect the user to enter `EOF`? Would'nt a newline character be sufficient? – Andreas Wenzel Apr 03 '21 at 21:33
  • Sorry, not EOF but end of function as in there is no more input, or when the input test returns 0. Thank you guys! – Drew Mortenson Apr 03 '21 at 21:35
  • Apart from `Input` being outside the loop, if you type a capital, tab, space, (UTF-8!), _etc_, the programme will behave strangely. Also this is a strictly surjective function that does not guarantee decyptabitity; suggest a cycle of mappings and default print whatever was given. – Neil Apr 04 '21 at 04:16
  • switch is a control structure, not a function – phuclv Apr 04 '21 at 14:01

1 Answers1

1

The simplest solution would be to remove the line

Input = getchar();

and to replace the line

while (Input != EOF) {

with:

while ( (Input=getchar()) != EOF && Input != '\n' ) {

Alternatively, if you find this while condition too confusing, you could also use an infinite loop, instead, like this:

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

int main( void )
{
    printf("Please type your message\n");

    for (;;) //infinite loop, equivalent to while(true)
    {
        int c;

        c = getchar();

        if ( c == EOF || c == '\n' )
            break;

        switch ( c )
        {
           case 'a':printf("!"); break;
           case 'b':printf("@"); break;
           case 'c':printf("#"); break;
           case 'd':printf("$"); break;
           case 'e':printf("%%"); break;
           case 'f':printf("^"); break;
           case 'g':printf("&"); break;
           case 'h':printf("*"); break;
           case 'i':printf("`"); break;
           case 'j':printf("~"); break;
           case 'k':printf("-"); break;
           case 'l':printf("_"); break;
           case 'm':printf("="); break;
           case 'n':printf("+"); break;
           case 'o':printf("["); break;
           case 'p':printf("{"); break;
           case 'q':printf("]"); break;
           case 'r':printf("}"); break;
           case 's':printf(";"); break;
           case 't':printf(":"); break;
           case 'u':printf("|"); break;
           case 'v':printf(","); break;
           case 'w':printf("<"); break;
           case 'x':printf("."); break;
           case 'y':printf(">"); break;
           case 'z':printf("'"); break;
        }
    }

    return 0;
}

Note that most character sets (such as ASCII) store the characters a to z consecutively. With these character sets, you don't need the long switch statement. Instead, you can simplify it to the following:

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

int main( void )
{
    printf("Please type your message\n");

    for (;;) //infinite loop, equivalent to while(true)
    {
        const char map[] = "!@#$%^&*`~-_=+[{]};:|,<.>'";
        int c;

        c = getchar();

        if ( c == EOF || c == '\n' )
            break;

        if ( 'a' <= c && c <= 'z' )
            putchar( map[c-'a'] );
    }

    return 0;
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39