-4
char convertalphas(char s) {

switch (s){
    case 'A':
        return '0';
        break;
    case 'B':
        return '1';
        break;
    case 'C':
        return '2';
        break;
    case 'D':
        return '3';
        break;
    case 'E':
        return '4';
        break;
    case 'F':
        return '5';
        break;
    case 'G':
        return '6';
        break;
    case 'H':
        return '7';
        break;
    case 'I':
        return '8';
        break;
    case 'J':
        return '9';
        break;
    case 'K':
        return '10';
        break;
    case 'L':
        return '11';
        break;
    case 'M':
        return '12';
        break;
    case 'N':
        return '13';
        break;
    case 'O':
        return '14';
        break;
    case 'P':
        return '15';
        break;
    case 'Q':
        return '16';
        break;
    case 'R':
        return '17';
        break;
    case 'S':
        return '18';
        break;
    case 'T':
        return '19';
        break;
    case 'U':
        return '20';
        break;
    case 'V':
        return '21';
        break;
    case 'W':
        return '22';
        break;
    case 'X':
        return '23';
        break;
    case 'Y':
        return '24';
        break;
    case 'Z':
        return '25';
        break;
 }
}

int main()
{

char astring[10];
int i = 0;
int flag = 0;
int startedalpha = 0;
//check if there is a digit input or not
int nodigit = 0;
char cell[10];
int col;
scanf( "%s", &astring );
for ( i = 0; i < 10; ++i )
{
if(astring[i] != '\0') {

//check whether letter is capital or small
 if (astring[i] >= 65 && astring[i] <= 90)
 {
        startedalpha = 1;

        //printf( "%c\n", astring[i] );
        cell[i] = convertalphas(astring[i]);

        printf("cell is %s\n", cell);
 }

What im trying to do is to concatenate all of my conversions for a later use. when I put "AB" it returns "01@" and when I put "Z" it returns something else than "25". I don't know what is wrong but it is driving me crazy! I want to be able to input "ABZ" and it saves all of my values into the variable cell. For example, "ABZ" "0125"

Thanks!

I'm confused with this forum. Obviously, I posted the question because there is something I don't know and I'm not a guru as many of you! So, why would I get -2? I already posted what I tried. I thought it is about helping not being condescending!

Thanks for those who replied anyway!

Edit --

I converted my switch statement to int but now how can I concatenate the integers in variable cell?

Spun a
  • 35
  • 1
  • 5
  • 4
    Uh, `'12'` isn't a legal character. – Hot Licks Nov 03 '14 at 17:36
  • How can you return `25`, 2 chars, when you're only supposed to return one char when you enter `Z`? – AntonH Nov 03 '14 at 17:36
  • 4
    (Surely the compiler was spitting out warning messages. Perhaps you should read them.) – Hot Licks Nov 03 '14 at 17:37
  • You should reconsider this code and rewrite the `switch/case` part. Hint: the rewritten code will take < 5 lines. – Jabberwocky Nov 03 '14 at 17:40
  • Compiling that function on coliru: http://coliru.stacked-crooked.com/a/cf5b241bb1b75ba4 Look at that bunch of warnings! – Deduplicator Nov 03 '14 at 17:40
  • Instead of returning chars why don't you return int's that you convert to a string? You would also need to update `cell` to be an integer array, but as it stands char isn't the type you want for `12` in any case. – scrappedcola Nov 03 '14 at 17:40
  • 1
    `'10'`, `'11'`, etc. are legal multi-character constants. Unlikely code needs these. Suggest a new approach. – chux - Reinstate Monica Nov 03 '14 at 17:48
  • @MichaelWalz (as long as you are using a character encoding that lays out `'A'...'Z'` consecutively.) – The Paramagnetic Croissant Nov 03 '14 at 17:49
  • `cell[i]` will be undefined in the case where the string char is not a capital letter. – Weather Vane Nov 03 '14 at 17:52
  • They are multicharacter literals and don't do what you thought http://stackoverflow.com/questions/3960954/c-multicharacter-literal?rq=1 http://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters http://stackoverflow.com/questions/7755202/multi-character-constant-warnings Turn on all warning and read them first – phuclv Nov 03 '14 at 18:20
  • @TheParamagneticCroissant : I'm not aware of a charset where A-Z is not consecutive, and even then, it should be done with a table and a 6-8 lines function. – Jabberwocky Nov 03 '14 at 21:39
  • @MichaelWalz in EBCDIC the characters are not consecutive – phuclv Nov 05 '14 at 08:12

4 Answers4

1

According to the C Standard (6.4.4.4 Character constants)

  1. ...The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.All

All return values of the function starting from '10' to '25' inclusively are imolementation defined and you can get the result that you did not expect to get.

You need to write a function that would be declared like

char * convertalphas( const char *s );

Or

char * convertalphas( char *dest, const char *source );

By the way it will be difficult to make the reverse conversion. For example what does "25" mean? Whether it is "CF" or "Z"?:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

One of things I noticed is that you have a char returning function, and is returning something else than a char, like '10'. It will return an unexpected value. You can, for example, set the return of this function to integer. So it will look like this:

int convertalphas(char s) {
  switch (s){
    case 'A':
        return 0;
    case 'B':
        return 1;
    case 'C':
        return 2;
    case 'D':
        return 3;
    case 'E':
        return 4;

  return -1;
}

And then, change inside the loop in main function:

sprintf(auxvar, "%d", convertalphas(astring[i]);
for( jj = 0; jj < strlen(auxvar); jj++)
  cell[i++] = auxvar[jj];

I hope it can help you somehow! Good Luck.

rfermi
  • 179
  • 11
  • Thank you! But it doesn't work. When I put AB, it returns wrong value. – Spun a Nov 03 '14 at 18:04
  • @Spuna What do you mean, put AB? You can't pass a two character string to a function accepting a char. If you mean you passed a `'AB'`, i.e. a multibyte char, then you need to look into `wchar_t` support. Your biggest problem seems to be that you didn't crank up the warning level of your compiler. It would have told you on the spot that convertalphas doesn't return a value for all paths. – Jens Nov 03 '14 at 18:09
  • Then how can I pass two character string? Cause that's what I'm trying to do. – Spun a Nov 03 '14 at 18:14
  • @Spuna By understanding that strings in C are arrays of char, terminated by a NUL byte. Your function should accept a `char *` and deal with the charaters appropriately. The best book to learn C is *Kernighan and Ritchie, The C Programming Language, 2nd ed.* – Jens Nov 03 '14 at 18:23
0

Apart from the values returned by chars 'K' thru 'Z' (as mentioned by others) there are some other errors with the use of cell

Each character returned from your function is written into cell[] and then you print cell as a string. But there is no string terminator. You either need to fill cell[] with zeros first, or write a 0 into the next character

cell[i] = convertalphas(astring[i]);
cell[i+1] = 0;

But the problem with this is that cell[i] is skipped when the char is not a capital letter. You need to maintain a separate index into cell[]

cell[j++] = convertalphas(astring[i]);
cell[j] = 0;

One more problem is cell[] is not long enough for the final string terminator. You need to declare it as

char cell[11];
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

To summarize, your problem is:

  1. A char function cannot return anything other than a single character.
    If you try to return something like '12', it just simply doesn't work. You need to rethink your algorithm. Implementing this using ints should be very straightforward.

  2. Is this a homework assignment requiring the use of the switch statement? If not, and if you follow the advice to return int instead of char, a simple s-65 would do the trick, you don't need a 78-line switch.

  3. Since cell is a char array, each element of cell can also only store a single character.
    It is not very clear what you are trying to achieve, so I will try to comprehend it in two ways:

    3.1. You want to store each character's value separately in cell.
    Example: for input "ABZ", you want cell to be {0, 1, 25}.
    This is easy. Change cell to an int array, and output them using %d format specifier in your printf.

    3.2 You treat cell as a string rather than an array of characters, and you concatenate the string for every character's value.
    Example: for input "ABZ", you want cell to be "0125", or in other words, {'0', '1', '2', '5'}. This means that you won't be able to differentiate between "ABZ" and, say, "AMF", as others have pointed out.
    If this is what you want to achieve, firstly 10 elements is not enough for cell - each character must be stored separately, and you cannot store "25" as two characters in a single element of cell.

You can assign it with, say, 21 elements (2 max for each alphabet, one extra for the terminating '\0' byte). After each alphabet is converted to its value in int, implement a counter and some conditionals to fill cell one character at a time. Example, if an alphabet's value is stored in val:

char cell[21];
int i=0;
if(val < 10) {
    cell[i] = val + '0';
    i++;
} else {
    cell[i] = val / 10 + '0';
    i++;
    cell[i] = val % 10 + '0';
    i++;
}

I'll leave the implementation of this into a loop as an exercise for you...

user12205
  • 2,684
  • 1
  • 20
  • 40