-3

I am trying to create an application that can replace a character to a number. Let say A = 2 and F=3 if I write AFAF = 2323 should be the result, Please help.

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

int main(){

char *test;
char *result;
int i,e = 0;
int ch;

    while((ch = getchar()) != '\n'){
            if(e < 5){
                    *test++=ch;
                    e++;
            }
    }
    *test = '\0';

    for(i =0; i < 5; i++){
            if(strcmp(test++,"A") == 0 || strcmp(test++,"B")==0 || strcmp(test++,"C")==0){
                    result[i] = "2";
            }else if (strcmp(test++,"D") == 0 || strcmp(test++,"E")== 0 || strcmp(test++,"F")== 0){
                    result[i] = "3";
            }
    }
    for(i = 0; i<5;i++){
            printf("%s", result[i]);
    }
return 0;
}
Test
  • 1
  • 1
  • 2
    Why use `strcmp` for character-based comparison? – iBug Apr 09 '18 at 03:05
  • 1
    You should just use `something == 'A'` for your purpose. There are also several other errors that you need to fix before your program will run. For example uninitialized pointers, wrong format specifier in `printf`, wrong pointer type in assignment, etc. – iBug Apr 09 '18 at 03:07
  • You are not allocating memory ( Calling malloc ) for test and result variables and using them.. – md.jamal Apr 09 '18 at 04:17

1 Answers1

0

I modified you code for it to work, not fully tested:

  1. you are missing sanity checks on the input
  2. no need for strcmp on char based comparisons
  3. you are using pointers to arrays - but you do not allocate memory for them see malloc documentation and examples - mu code uses static allocation so i avoid using them
  4. test++ will change the value when it is evaluated so your comparing section.. well it is just wrong it doesn't work
  5. if you use %s no need for a loop as you are printing a string - which mus be null terminated (\0) if you want to print the chars and avoid null termination use %c instead

I hope this code will give you something to work with


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

int main(){

    char test[6]; //your test array is of fixed size + 1 for the '\0' char
    char result[6]; //if you use a pointer you must malloc the array - but in this case use static allocation it is easier
    int i,e = 0;
    int ch;

    while((ch = getchar()) != '\n'){
        if(e < 5){
            test[e]=ch; //no need for pointer here
            e++;
        }
    }
    test[e] = '\0'; //null terminator at the end of the string - not really needed at all

    printf("%s", test);
    for(i =0; i < 5; i++){
        if ( test[i] == 'A' || test[i] == 'B' ||test[i] == 'C' )
            result[i] = '2';
        else if ( test[i] == 'D' || test[i] == 'E' ||test[i] == 'F' )
            result[i] = '3';
    }
    result[5] = '\0'; //add the null terminator - only needed if you wish to print with %s

    /* you can just print the string no need for a loop here */
    printf("%s", result);
    return 0;
}
Omer Dagan
  • 662
  • 5
  • 14