0

My logic seems to be working good. But the problem comes near the symbols and spaces. Can you help me why?

#include<stdio.h>
#include<ctype.h>
#include<cs50.h>
#include<string.h>
int main(int argc,char* argv[1])
{
    char y;
    if(argc==2)
    {
        int k = atoi(argv[1]);
    printf("plaintext:");
    string s=get_string();
   int l=strlen(s);
      printf("ciphertext: ");
   for(int b=0;b<l;b++)
  {
if(isalpha(s[b]))
{
    if(isupper(s[b]))
    {
        for(int a=0;a<l;a++)
        {
            s[a]=s[a]-65;
            s[a]=(s[a]+k)%26;
             y =s[a]+65;
             printf("%c",y);
        }
    }
   else
    {
        for(int a=0;a<l;a++)
        {
            s[a]=s[a]-97;
            s[a]=(s[a]+k)%26;
             y=s[a]+97;
             printf("%c",y);
        }
    }
}
 else
   {
      printf("%c",s[b]);
   }
 }

  printf("\n");
  return 0;

}
 else
 {
     printf("Enter the correct parameters\n");
     return(1);
 }
}

Hope you can help!!

0

Why nested loops? There's no use for your a loop. Just process s[b].

2
  • thanks can you tell me why? it woudnt read the spaces and symbols with a nested loop? isnt the logic correct? just wanna know. the program works now though. Apr 3 '18 at 15:32
  • You need to go through your string using one loop. You would examine a single character at a time, either print a version processed in some way, or print the character itself if it's not a letter. I don't see how there would be any use for a second loop. Loop outside, some structure with if-else on the inside. Alternatively, printing could be done after the loop, then you would need to store the altered letters (for example in the original string as you no longer need that, or in an array). But the loop-if-else structure remains.
    – Blauelf
    Apr 3 '18 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .