0

The code takes input(the string) to be encoded and a key from the user such that when the key is added to the input the input is increased by the amount of the given key . For ex. if the key is 2, so the input A changes to C, b changes to d, and so on.

I have written a code for the same but cannot get the output.

int main()
{
  int x,i,y,c;
  char text[20];

  printf("enter the plaintext:");
  gets(text);
  printf("enter the key: ");
  scanf("%d",&x);
  for (y=0;y<strlen(text);y++)
  {
    if (text[i]>='a'&&text[i]<='z'&&text[i]>='A'&&text[i]<='Z' )
    {
      int c=(int)text[i]+x;
      printf("%c\n",text[i]);
    }
  }
}

The result that i am getting is blank. kindly help me.

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 3
    Never *ever* use the `gets` function. It's a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function and has therefore been removed from the C standard. Use [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead (but be aware of its differences). – Some programmer dude Feb 09 '19 at 15:35
  • 4
    Could you give an example of a value of `text[i]` that would satisfy your `if` condition? – ForceBru Feb 09 '19 at 15:36
  • I also recommend you check out [the `isalpha` standard function](https://en.cppreference.com/w/c/string/byte/isalpha). – Some programmer dude Feb 09 '19 at 15:36
  • Note that you have to deal with wraparound too. For example, if the key is 10 and the letter is `Z`, you have to translate that to `J`, but your code does not do that. – Jonathan Leffler Feb 09 '19 at 16:17
  • How can text[i] be in [a-z] and also in [A-Z]? – jarmod Feb 09 '19 at 18:00

2 Answers2

3

There are a lot of problems in your proposal, it is needed to check the inputs success, you iterate on y rather than on i, you compute the new char code but you do not print it

Here a corrected proposal :

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

int main()
{
  char text[100];

  printf("enter the plaintext:");

  if (fgets(text, sizeof(text), stdin) != NULL) {
    int key;

    printf("enter the key: ");
    if (scanf("%d", &key) == 1) {
      int i;

      for (i=0; text[i] != 0; ++i)
      {
       if (isalpha(text[i]))
         putchar(text[i] + key); /* may not be printable */
       else
         putchar(text[i]);
      }
    }
  }
  return 0;
}

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra c.c
pi@raspberrypi:/tmp $ ./a.out
enter the plaintext:the sentence to encode
enter the key: 3
wkh vhqwhqfh wr hqfrgh
pi@raspberrypi:/tmp $ ./a.out
enter the plaintext:Alea jacta est
enter the key: 2
Cngc lcevc guv

For the fun, 32 is not a very good key to encode uppercase characters :

pi@raspberrypi:/tmp $ ./a.out
enter the plaintext:THE FIVE BOXING WIZARDS JUMP QUICKLY.
enter the key: 32
the five boxing wizards jump quickly.
bruno
  • 32,421
  • 7
  • 25
  • 37
  • Note that you'd get 'interesting' results if the key was larger, or the sentence was more like one of these: "The quick brown fox jumps over the lazy dog." — "Pack my box with five dozen liquor jugs." — "The five boxing wizards jump quickly." — "How vexingly quick daft zebras jump." — "Bright vixens jump; dozy fowl quack." – Jonathan Leffler Feb 09 '19 at 16:15
  • @JonathanLeffler because it was about Caesar what about some Latin sentences ? "Alea jacta est" and 5 -> "Fqjf ofhyf jxy" ^^ – bruno Feb 09 '19 at 16:22
  • The advantage of the sentences I cited is that they cover the entire 'Latin' alphabet (bearing in mind that Latin didn't use all the letters used in the modern Latin alphabet). – Jonathan Leffler Feb 09 '19 at 16:24
  • @JonathanLeffler ah ok, I understand now their interrest – bruno Feb 09 '19 at 16:27
  • @bruno I didn't get the point that in this line "putchar(text[i] + key);", putchar uses a character "text[i]" and integer"key", then how can it add integer and character and give the correct result? – Gagan Batra May 13 '19 at 20:07
  • @bruno I didn't know earlier. Done. – Gagan Batra May 14 '19 at 17:30
  • @GaganBatra that is fine, happy coding – bruno May 14 '19 at 17:37
1

You initialized the y variable instead of i variable
try this :

for (i = 0; i < strlen(text); i++) { ... }

Note :
if you compile your code with the following flags -Wall -Wextra -Werror it will help you so much to know more about errors you might have, like unused variable.
example : gcc -Wall -Werror -Wextra youprogram.c -o output

you have other errors not just this one,
so i suggest to you my solution to your problem : (all output characters will be printable)

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

int     main(void)
{
    char    text[250];
    size_t  i;
    int     key;

    printf("Enter the plaintext : ");
    if (fgets(text, sizeof(text), stdin) != NULL)
    {
            printf("Enter the key : ");
            scanf("%d", &key);
            for(i = 0; i < strlen(text); i++)
            {
                    if (text[i] >= 'a' && text[i] <= 'z')
                            putchar((((text[i] - 'a') + key) % 26) + 'a');
                    else if (text[i] >= 'A' && text[i] <= 'Z')
                            putchar((((text[i] - 'A') + key) % 26) + 'A');
                    else
                            putchar(text[i]);
            }
    }
    return (0);
}
Zakariaa Oulhafiane
  • 407
  • 1
  • 4
  • 11
  • That's part, but only part, of the trouble. – Jonathan Leffler Feb 09 '19 at 16:13
  • but what is the use of subtracting 97 at first and then adding again, because anyway the remainder will be same. – Gagan Batra Feb 09 '19 at 19:04
  • not in all the time the same result, if you have the char 'd' for example who has ascii number is 100, the result will be the same because: (((100 - 97) + 3) % 26) + 97 = 103, but if you have the char 'z' for example who has the ascii number 122 the rezult will be different try to calculate it: (((122 - 97) + 3) %26) + 97 = 99 (the char 'c'), on that way we will loop over the alphabetic. – Zakariaa Oulhafiane Feb 09 '19 at 20:25