Problem in condition to handle string

1

It was meant to work only with lowercase letters and when it came to symbols did not change, but they are being altered. What's the problem?

 #include <stdio.h>
 int main()
 {
char texto[100];
int i = 0,j = 0,con,tam;
printf("texto\n");
gets(texto);
fflush(stdin);
printf("constante\n");
scanf("%d",&con);
tam = strlen(texto);
char cesar[tam];
for(i = 0; i<tam; i++)
{
    if(texto[i] >= 'a' || texto[i]<= 'z')
        cesar[i] = ( (texto[i] - 97 + con)%26 + 97);
    else
        cesar[i] = texto[i];

    printf("%c",cesar[i]);
}
}
    
asked by anonymous 18.10.2015 / 17:51

1 answer

3

The problem is the condition that can not be OR, it should be AND, as it was the condition would always be true.

#include <stdio.h>
#include <string.h>
int main() {
    char texto[100];
    int i = 0, con = 0, tam;
    printf("texto\n");
    fgets(texto, 100, stdin);
    printf("constante\n");
    scanf("%d", &con);
    tam = strlen(texto);
    char cesar[tam];
    for(i = 0; i < tam; i++) {
        if(texto[i] >= 'a' && texto[i] <= 'z')
            cesar[i] = ((texto[i] - 97 + con) % 26 + 97);
        else
            cesar[i] = texto[i];

        printf("%c", cesar[i]);
    }
}

See working on ideone .

I made other major improvements. But there are a few more that could be done.

    
18.10.2015 / 18:04