0

`

#include <stdio.h>
#include <string.h>

int main(void)
{
    int proceed,cipher;
    printf("Press 1 for decode and 0 to encode:- ");
    scanf("%i",&proceed);
    

    if (proceed)
    {
        char ciphertext[100];
        printf("Enter the cipher key:- ");
        scanf("%i",&cipher);
        printf("Enter CipherText:- ");
        scanf("%s",ciphertext);
        int len =  strlen(ciphertext);
        printf("Ciphertext:- ");

        for(int i = 0; i<len; i++)
        {
            if((ciphertext[i]) != ' ')
            {
                printf("%c", ciphertext[i]-cipher);
            }
            
        }
        printf("\n");
    }else{
        char plaintext[100];
        printf("Enter the cipher key:- ");
        scanf("%i",&cipher);
        printf("Enter PlainText:- ");
        scanf("%s",plaintext);
        int len =  strlen(plaintext);
        printf("Ciphertext:- ");
   
        for(int i = 0; i<len; i++)
        {
            if ((plaintext[i]) != ' ')
            {
                printf("%c", plaintext[i]+cipher);
            }
            
        }
        printf("\n");
    
    }
    return 0;
}

` But when i try to input sentences the program outputs only the first word. For example: when i input Press 1 for decode and 0 to encode:- 0 Enter the cipher key:- 2 Enter PlainText:- abc d Ciphertext:- cde and not ... Ciphertext:- cde f

I tried putting if ((plaintext[i]) != ' ') into the program but it still gives the same output where the characters after the space doesnt give any output.

  • Note that for `scanf` the format `%s` will not read any spaces (it read space-delimited "words"). So the condition `ciphertext[i] != ' '` will *always* be true. If you want to read whole lines, use `fgets` instead (and I actually recommend that for forget that `scanf` even exist, it's usually harder to get it to work as expected then one believes). – Some programmer dude Oct 26 '22 at 06:38

0 Answers0