How to change every letter in a string with the letter following it in the alphabet using C

1 Answer

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

char* changeLettersWithFollowingLetter(char* str) {
    char* result = malloc(strlen(str) + 1);
    
    int i = 0;
    while (str[i] != '\0') {
        int asciiCode = (int)str[i] + 1;
        
        if ((asciiCode >= 97 && asciiCode <= 122) || (asciiCode >= 65 && asciiCode <= 90)) {
            result[i] = (char)asciiCode;
        } else {
            result[i] = str[i];
        }
        
        i++;
    }
    
    result[i] = '\0';
    
    return result;
}

int main() {
    char* result = changeLettersWithFollowingLetter("8a Frk");
    
    printf("%s\n", result);
    
    free(result);
    
    return 0;
}


 
 
/*
run:
    
8b Gsl
    
*/

 



answered Mar 1, 2024 by avibootz

Related questions

...