0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int count=0;
int main(){
    int n; 
    scanf(" %d",&n);
    char *s = (char *)malloc(10240 * sizeof(char));
    scanf(" %s",s);
    int length=strlen(s);
    char array[length+1];
    int k; 
    scanf(" %d",&k);
    while(*s!='\0')
        {

        char a= *s + k;
        if(a>90 && *s<=90)
            {
            a=65+(k-1);
            array[count]=a;
        }
        else if(a>122 && *s<=122)
            {
            a=97+(k-1);
            array[count]=a;
        }
        else if (a >=0 && a<=64)
            {
            array[count]=a;
        }
       else if(a>=123 && a<=126)
           {
           array[count]=a;
       }

        count++;
        s++;


    }
    array[count+1]='\0';
    printf("%s",array);
    return 0;
}

In the below code ,Each unencrypted letter is replaced with the letter occurring 'k' spaces after it when listed alphabetically. Think of the alphabet as being both case-sensitive and circular; if 'k' rotates past the end of the alphabet, it loops back to the beginning (i.e.: the letter after 'z' is 'a' , and the letter after 'Z' is 'A' ).

I am unable to get where I am going wrong ,it is printing null for every possible input sequence of letters .The special symbols don't change in the encrypted string .

Radha Gogia
  • 765
  • 1
  • 11
  • 22
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Oct 17 '16 at 18:31
  • ...especially since you never included ``, mandatory for proper usage of `malloc`. And related, your `array[]` is undersized. You leave no space for the terminator. – WhozCraig Oct 17 '16 at 18:34
  • Even if I include it , it is not working as desired ,no changes in output. – Radha Gogia Oct 17 '16 at 18:36
  • Since you create a static-, fixed-size buffer that does not need to survive the function returning, it would be much easier and cleaner to declare it as an *array*: `char s[10240];`. 10 KB is unlikely to be too much for the stack. – John Bollinger Oct 17 '16 at 18:37
  • On the other hand, just because you make the buffer big does not mean it is safe from being overrun. Specify a suitable field width in your `scanf()` format to ensure that that does not happen. – John Bollinger Oct 17 '16 at 18:38
  • strlen gives the entire length of the string including null character and even if I declare array size as length+1 , still I am not getting desired output . – Radha Gogia Oct 17 '16 at 18:38
  • I have edited the code , please see still I am getting some garbage characters . – Radha Gogia Oct 17 '16 at 18:59
  • `strlen` does not include the NULL character in its returned length – yano Oct 17 '16 at 19:09
  • Please do not delete questions which people are trying to answer, and then make a bunch of corrections before re-enabling. And do not use "magic numbers" in the code. if `122` means anything in context, it is `'z'`. – Weather Vane Oct 17 '16 at 19:13
  • Time to study encodings, especially character encodings. – zaph Oct 17 '16 at 19:17
  • I suspect the first problem is in that clusterf in between `main` and your `while` loop. Start by hardcoding the word you want to encode as well as the `k` value and work backwards from there. What is `n`? Why is `count` outside of `main`? – yano Oct 17 '16 at 19:30

1 Answers1

0
k = 2; //<== random input
while (*s != '\0')
{
    printf("count %d\n", count);
    char a = *(s + k);
    ...
}

This loop goes to the end of the string s, and char a tries to extract the character which is k positions after the current index.

So when you are at the end of the string, *(s + k) is out of bound. I recommend rewriting the code as follows:

int i;
for (i = 0; i < length; i++)
{
    if (i + k == length) break;
    if (i + k < 0) break; //assuming k can be negative
    char a = s[i + k];
    ...
}

This way i+k is always a valid index between 0 and length.

Another problem is here:

if (a>90 && *s <= 90)
{
    a = (char)(65 + (k - 1));
    array[count] = a;
}
else if (a>122 && *s <= 122)
{
    a = (char)(97 + (k - 1));
    array[count] = a;
}
else if (a >= 0 && a <= 64)
{
    array[count] = a;
}
else if (a >= 123 && a <= 126)
{
    array[count] = a;
}
count++;

There is no guarantee that at least of these conditions is met. In fact it is likely that all of the conditions are false. So what happens is that array[0] remains uninitialized, but count is still incremented, therefore array contains garbage at the end. It should be changed so that count is incremented only if array[count] is set.

For Caesar encryption you want to shift the characters to left or right. You can do this by use % operator.

strcpy(s, "ABCDEFGZ");
int length = strlen(s);
char *array = malloc(length + 1);
int shift = 2;
int i;
for (i = 0; i < length; i++)
{
    char c = s[i];
    if (c >= 'A' && c <= 'Z')
        c = 'A' + (c - 'A' + shift) % 26;
    array[i] = c;
}
array[i] = 0;

Note when 'Z' is shifted to the right, the % operator makes sure it get back in to range between 'A' to 'Z'

The above function is not complete, it won't work for lower case letters, that's for your homework...

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77