3

I need to replace some chars with multiple chars (a string), but I got stuck. This code works for replacing one char with another, but if the replacement are multiple chars, the output messes up.

Here is the code I have so far:

char input[50];
char output[150];
int i;
printf("Enter your text: ");
fgets(input, 50 , stdin);

for (i = 0; input[i] != '\0'; i++){
    switch (input[i]){
        case 'a': output[i]= '4'; break;
        case 'd': output[i]= '|)';break;
        case 'e': output[i]= '3'; break;
        case 'f': output[i]= '|='; break;
        case 'u': output[i]= '|_|'; break;
        case 'w': output[i]= '\|/'; break;
        /* REST OF THE ALPHABET 
        INTENTIONALLY SUPPRESSED*/
    }
}
printf("Your new text is: %s", output);
return 0;

As suggested by dasblinkenlight, I set another index for the output, That worked pretty fine, but I getting two additional chars at the end of the output text... where does those chars come from?

This is an example:

Enter your text: afedef Your new text is: 4|=3|)3|=■(

  • let's say a string is a row of watermallons wrapped with boxes. if you want to pull out a watermellon and replace it with a different one, there is no problem. but if you want one watermellon to be replaced with 3, you need to put extra 2 boxes each time . in our example , a byte is the box and the character is the watermellon – David Haim May 25 '15 at 18:16
  • No need to use fruits. I understood that from the examples I saw. So, I tried adding "spaces" to the output index, but that did't work. – Panchístiko N. May 25 '15 at 18:21
  • Please *do not attempt* to stuff your code into the humble comment field. Just [edit] your post and add relevant information to it. – Jongware May 25 '15 at 18:24

4 Answers4

0

There are two problems in your code:

  • you are using i to iterate the output array. The size of the input array and output array are different. It may happen (e.g. in the case of f) that the index for the input array needs to be incremented by 1, whereas the index for the output array should be incremented by 2 or 3.

  • you can't assign multiple chars to a single char slot in an array. For instance, case 'f': output[i]= '|='; break; is incorrect.

To solve the problem you should use another variable and increment it with the number of characters added. For example:

int j = 0;
...
  case 'e': 
   output[j++]= '3'; 
   break;
  case 'f': 
   output[j++]= '|'; 
   output[j++]= '='; 
   break;
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
0

1) You can't copy strings with = operator.
2) String literal in c is surrounded with double quotes
3) Special characters like backslash ('\') in string literals have a special meaning and should be escaped.
4) In order to do what you intend you will need:
a) Additional counter to track the position in the output string, where the next string will be written
b) use of strcpy/strncpy instead of assignment operator = , such that each line in the `switch statement will look similar to this:

case 'f': strcpy(&output[j], "|="); j+=2; break;

Here j is the second counter incremented by the number of characters written to output

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
0

Try the following

#include <string.h>

//...

char *p = input, *q = output;

do    
{
    switch ( *p )
    {
        case '\0': *q = *p; break;
        case 'a': *q++ = '4'; break;
        case 'd': strcpy( q, "|)" ); q += 2; break;
        case 'e': *q++ = '3'; break;
        case 'f': strcpy( q, "|=" ); q += 2; break;
        case 'u': strcpy( q, "|_|" ); q += 3; break;
        case 'w': strcpy( q, "\|/" ); q += 3; break;
        /* REST OF THE ALPHABET 
        INTENTIONALLY SUPPRESSED*/
    }
} while ( *p++ );

Or instead of using strcpy you can assign each caharcter one after another as for example

        case 'd': *q++ = '|'; *q++ = ')'; break;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Take a look at strcat (http://www.cplusplus.com/reference/cstring/strcat/):

char input[50];
char output[150];
int i;
printf("Enter your text: ");
fgets(input, 50 , stdin);
output[0] = 0; // null terminate before calling strcat.

for (i = 0; input[i] != '\0'; i++)
{
    switch (input[i])
    {
        case 'a': strcat(output, "4"); break;
        case 'd': strcat(output, "|)"); break;
        //...
    }
}
printf("Your new text is: %s", output);

Also, multiple characters in single quotes in C++ is implementation defined (C++ multicharacter literal), you'll probably want to avoid them. Also, be careful when using '\' in literals (as with your last case), it's probably not doing what you expect.

Community
  • 1
  • 1
MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78