Swap letter by number in C

Asked

Viewed 1,472 times

2

I’m trying to program a phone algorithm with the old keys where the user will enter the letters as input and output will return me in the form of numbers. Note an example below:

Input: Hello-World

Output: 43556-96753

I tried with the code below, but only returns me the number of the first letter of the word

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

int main(){
    char texto[30];

    printf("Pressione A B C para 2");
    printf("\nPressione D E F para 3");
    printf("\nPressione G H I para 4");
    printf("\nPressione J K L para 5");
    printf("\nPressione M N O para 6");
    printf("\nPressione P Q R S para 7");
    printf("\nPressione T U V para 8");
    printf("\nPressione W X Y Z para 9");
    printf("\n\nInforme as letras: ");
    gets(texto);

    char *ptr;
    ptr = strtok(texto, "-");

    while (ptr != NULL){
        if (*ptr == 'A' || *ptr == 'B' || *ptr == 'C'){
            printf("2");
        }
        else if (*ptr == 'D' || *ptr == 'E' || *ptr == 'F'){
            printf("3");
        }
        else if (*ptr == 'G' || *ptr == 'H' || *ptr == 'I'){
            printf("4");
        }
        else if (*ptr == 'J' || *ptr == 'K' || *ptr == 'L'){
            printf("5");
        }
        else if (*ptr == 'M' || *ptr == 'N' || *ptr == 'O'){
            printf("6");
        }
        else if (*ptr == 'P' || *ptr == 'Q' || *ptr == 'R' || *ptr == 'S'){
            printf("7");
        }
        else if (*ptr == 'T' || *ptr == 'U' || *ptr == 'V'){
            printf("8");
        }
        else if (*ptr == 'W' || *ptr == 'X' || *ptr == 'Y' || *ptr == 'Z'){
            printf("9");
        }

        ptr = strtok(NULL, "-");
    }

    return 0;
}

2 answers

1


This code doesn’t even compile, and it uses things that should no longer be used, and it’s still too complex, without even doing what it seems to want. If you want me to present the number for all letters you should do this with each one and not just with the first letter of the word as you did. You have to scan the entire text character by character, I see no point in using strtok(). And he said turn it into a capital letter to make sure that the conversion happens even if typed in a small case. It can simplify more, but it’s good like this:

#include <stdio.h>
#include <ctype.h>

int main(){
    char texto[30];
    printf("Pressione A B C para 2");
    printf("\nPressione D E F para 3");
    printf("\nPressione G H I para 4");
    printf("\nPressione J K L para 5");
    printf("\nPressione M N O para 6");
    printf("\nPressione P Q R S para 7");
    printf("\nPressione T U V para 8");
    printf("\nPressione W X Y Z para 9");
    printf("\n\nInforme as letras: ");
    scanf("%s", texto);
    for (int i = 0; texto[i] != '\0'; i++) {
        char letra = toupper(texto[i]);
        if (letra == 'A' || texto[i] == 'B' || letra == 'C') printf("2");
        else if (letra == 'D' || letra == 'E' || letra == 'F') printf("3");
        else if (letra == 'G' || letra == 'H' || letra == 'I') printf("4");
        else if (letra == 'J' || letra == 'K' || letra == 'L') printf("5");
        else if (letra == 'M' || letra == 'N' || letra == 'O') printf("6");
        else if (letra == 'P' || letra == 'Q' || letra == 'R' || letra == 'S') printf("7");
        else if (letra == 'T' || letra == 'U' || letra == 'V') printf("8");
        else if (letra == 'W' || letra == 'X' || letra == 'Y' || letra == 'Z') printf("9");
        else printf("%c", texto[i]);
    }
}

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

0

Follow another well-reduced solution based on the table ASCII

#include <stdio.h>
int main(int argc, char *argv[]) {

    char texto[30], result; int i = 0;
    printf("Pressione A B C para 2");
    printf("\nPressione D E F para 3");
    printf("\nPressione G H I para 4");
    printf("\nPressione J K L para 5");
    printf("\nPressione M N O para 6");
    printf("\nPressione P Q R S para 7");
    printf("\nPressione T U V para 8");
    printf("\nPressione W X Y Z para 9");
    printf("\n\nInforme as letras: ");
    scanf("%s", texto);
    while (texto[i] != '\0'){
        result = tolower(texto[i]) > 96 && tolower(texto[i]) < 122 ?  (char)((20 - tolower(texto[i]) / 122) + (5 * tolower(texto[i]) / 16)) : texto[i];
        printf("%c", result);
        i++;
    }
}

See working here

Browser other questions tagged

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