characters and ASCII table

0

I'm trying to perform a URI exercise, and in 1 part it asks for the following:

  

In the first pass, only characters that are lowercase and   capital letters should be moved 3 positions to the right, according to   ASCII table: letter 'a' should turn letter 'd', letter 'y' should turn   character '|' and so on

But he is also changing those that do not leave lowercase and uppercase letters, I do not know why this is happening, thank you who can help.

For example: with input texto #3 should exit wh{wr #3 , however leaving wh{wr <

#include <stdio.h>
#include <string.h>
int main(){
char x[10],y[10];
int i,qnt;
fgets(x,sizeof(x),stdin);
qnt = strlen(x);

 /********   1 PASSADA  ************/
 for(i = 0;i<qnt;i++){
    if (((x[i]>=65) && (x[i]<=90))  || ((x[i]>=97) && (x[i]<=122)) )
            y[i]=x[i]+3;
            printf("%d %d , %c \n",i,y[i],y[i]);
 }

 printf("tamanho da string %i \n",qnt);

 printf("normal %d , %s\n",x,x);
  printf("1 passada  , %s \n",y);

 printf("normal %d , %s\n",x,x);
 for(i = 0;i<qnt;i++){
    printf("%d %d , %c \n",i,x[i],x[i]);

 }
 }
    
asked by anonymous 11.03.2018 / 22:17

1 answer

1

Actually the program is not putting values for when it is not a letter, thus taking the default value that is in the y vector.

Change your for to:

for(i = 0; i<qnt; i++)
{
    if (((x[i]>=65) && (x[i]<=90))  || ((x[i]>=97) && (x[i]<=122)) )
        y[i]=x[i]+3;
    else //faltou este else
        y[i]=x[i]; //que atribui o valor original quando não é letra

    printf("%d %d , %c \n",i,y[i],y[i]);
}

Confirm Ideone that already gets the expected exit

Some comments that I leave:

  • If you have main as int you should put the return value at the end with return 0; indicating that the program ended successfully

  • Placing the ascii value of the letters directly is detrimental to the reading of the code. You should instead put the letter as char . Soon the if within for would be better written like this:

    if ((x[i]>='A' && x[i]<='Z')  || (x[i]>='a' && x[i]<='z') )
        y[i]=x[i]+3;
    
  • The printf("normal %d , %s\n",x,x); leaves you a string at the end of the reading within x . If you do not want it or it is imperative that you do not, remove it. You can see in this my other answer how do it .

11.03.2018 / 22:39