Print the letters in front of the alphabet

Asked

Viewed 154 times

1

The idea of the code was to enter a letter, for example 'a' and return a letter in front of the alphabet, in this case the letter 'b', but the program does not return anything and I can not find the problem

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

int main(){
    char str[100];

    fgets(str, 100, stdin);

    for (int i=0;i<strlen(str);i++){
        printf("%c", str[i]+1);
    }

    return 0;
}
  • theoretically is doing this just testing (has modifications to do in the code), but, it is working yes. (changes do not need the is, for example)

  • One point you have to consider is that as you speak of "letter" then you have to decide whether it will be circular or not, that is, whether the letter following a 'z' should or should not be a 'a''.

1 answer

0


You have to check if you found the line break, because that’s what the fgets() puts when you finish typing. You can also check whether you found the null terminator of the string.

The efficiency of the code was terrible, it had quadratic complexity, I made it linear, apart from the strlen(). And I corrected the mistake that would buffer.

If the code was used for other things it would have to change q line break to the terminator. It can be seen in Problem with strlen() and Text looks full of junk after typed.

#include <stdio.h>

int main() {
    char str[101];
    fgets(str, 100, stdin);
    for (int i = 0; str[i] && str[i] != '\n'; i++) printf("%c", str[i] + 1);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.