-1

I'm kinda stuck on this one. When I run my program all of the letters that loop past z do not print for some reason. The problem comes from this link: http://docs.cs50.net/2016/x/ap/problems/caesar/caesar.html Here is my code:

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

int main(int argc, string argv[])
{
    if (argv[1] != NULL)
    {
        int k = atoi(argv[1]);
        if ((argc == 2) && (k >= 0))
        {
            printf("Type what you would like to encrypt!\n");  
            string text = GetString();
            for (int i = 0, n = strlen(text); i < n; i++)
            {
                if (isalpha(text[i]) || isspace(text[i]))
                {
                    if (isupper(text[i]))
                    {
                        text[i] = text[i] + k;
                        if (text[i] > 'Z')
                        {
                            text[i] = text[i] - 26;
                            printf("%c", text[i]);
                        }
                        else
                        printf("%c", text[i]);
                    }
                    else if (islower(text[i]))
                    {
                        text[i] = text[i] + k;
                        if (text[i] > 'z')
                        {
                            text[i] = text[i] - 26;
                            printf("%c", text[i]);
                        }
                        else                        
                        printf("%c", text[i]);
                    }
                    else if (isspace(text[i]))
                    {
                        printf(" ");
                        if (isupper(text[i + 1]))
                        {
                            text[i + 1] = text[i + 1] + k;
                            if (text[i + 1] > 'Z')
                            {
                                text[i + 1] = text[i + 1] - 26;
                                printf("%c", text[i + 1]);
                            }
                            else
                            printf("%c", text[i + 1]);
                        }
                        else if (islower(text[i + 1]))
                        {
                            text[i + 1] = text[i + 1] + k;
                            if (text[i + 1] > 'z')
                            {
                                text[i + 1] = text[i + 1] - 26;
                                printf("%c", text[i + 1]);
                            }
                            else
                            printf("%c", text[i + 1]);
                        }
                        i++;
                    } 
                }
                else
                printf("%c", text[i]);
            }
            printf("\n");
            return 0;
        }
        else
        printf("Usage: ./caesar <non-negative integer>\n");
        return 1;        
    }
    else 
    printf("Usage: ./caesar <non-negative integer>\n");
    return 1;    
}

When I input "./caesar 13" (13 being the encryption key) into my terminal, I get prompted to input my desired text. However, when I input "matteo maTTeo", I get the output of: "znrb znGGrb". Why is this the case? I am pretty new to coding, so if the answer is obvious, go easy on me please! Thanks!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

0
                   ...
                    if (isupper(text[i + 1]))
                    {
                        text[i + 1] = text[i + 1] + k;
                        if (text[i + 1] > 'Z')
                        {
                            text[i + 1] = text[i + 1] - 26;
                            printf("%c", text[i + 1]);
                        }
                        else
                        printf("%c", text[i + 1]);
                    }
                    else if (islower(text[i + 1]))
                    {
                        text[i + 1] = text[i + 1] + k;
                        if (text[i + 1] > 'z')
                        {
                            text[i + 1] = text[i + 1] - 26;
                            printf("%c", text[i + 1]);
                        }
                        else
                        printf("%c", text[i + 1]);
                    }
                    i++;
                    ......

No need for this code in condition where you check for a space. It will be automatically handled in next iteration.

0

text[i] > 'z' : text[i] may be a negative. – BLUEPIXY

Fix: if ((unsigned char)text[i] > 'z')

Armali
  • 18,255
  • 14
  • 57
  • 171