Václav Klem
Václav Klem

Reputation: 11

Switch case messes up my array that im trying to convert

I'm new to C and im trying to convert letters to keys (as on old mobile keyboard) but the switch case somehow always ends up messing up the array. When I try to test the value it returns absolutely different character than there is in the array. e.g. character number 32 instead of capital V.

char *LetterToKey(char name[101])
{
    char number[101];
    bool unknwn = false;

    for(int i = 0; i<101; i++)
            {
                if(unkwn) break;
                switch (name[i])
                {

                case 'A':
                case 'B':
                case 'C':
                case 'a':
                case 'b':
                case 'c': number[i] = '2'; break;

                case 'D':
                //...

                default:
                    printf("%d\n", (int)name[i]);
                    unknwn = true;
                    break;
                }
            }
            return number;
}

Upvotes: 1

Views: 67

Answers (1)

user3629249
user3629249

Reputation: 16540

The following proposed code:

  1. cleanly compiles
  2. checks for errors
  3. requires the caller to pass the returned pointer to free()
  4. avoids the use of 'magic' numbers (like 101)
  5. properly terminates the resulting character array so it does not contain trash/uninitialized characters

and now, the proposed code:

#include <ctype.h>   // toupper()
#include <stdlib.h>  // calloc()
#include <stdio.h>   // printf()

#define MAX_BUF_LEN 101


char *LetterToKey( char *name )
{
    char *number = calloc( MAX_BUF_LEN+1, sizeof( char ) );
    if( !number )
    {
        return NULL;
    }

    // implied else, calloc successful

    size_t i = 0;

    while( i < MAX_BUF_LEN && number[ i ] ) 
    {
        switch ( toupper(name[i]))
        {
            case 'A':
            case 'B':
            case 'C':
                number[i] = '2'; 
                break;

            case 'D':
            case 'E':
            case 'F':
                number[i] = '3';
                break;

            // etc

            default:
                printf("unexpected char: %c\n", name[i]);
                number[ i ] = ' ';
                break;
        }
    }

    return number;
}

Of course, if the caller properly NUL terminated the input then the code can be simplified to:

#include <ctype.h>   // toupper()
#include <stdlib.h>  // calloc()
#include <stdio.h>   // printf()
#include <string.h>  // strlen()


char *LetterToKey( char *name )
{
    char *number = calloc( strlen( name )+1, sizeof( char ) );
    if( !number )
    {
        return NULL;
    }

    // implied else, calloc successful

    for( size_t i=0; number[ i ]; i++ ) 
    {
        switch ( toupper(name[i]))
        {
            case 'A':
            case 'B':
            case 'C':
                number[i] = '2'; 
                break;

            case 'D':
            case 'E':
            case 'F':
                number[i] = '3';
                break;

            // etc

            default:
                printf("unexpected char: %c\n", name[i]);
                number[ i ] = ' ';
                break;
        }
    }

    return number;
}

Upvotes: 1

Related Questions