Replace char with string
I need to replace some characters with multiple characters (string) but I am stuck. This code works for replacing one char with another, but if the replacement is multiple characters, the result is messy.
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, which worked pretty well, but I get two extra characters at the end of the output text ... where do these characters come from?
This is an example:
Enter your text: afedef Your new text: 4 | = 3 |) 3 | = ■ (
source to share
1) You cannot copy strings using an operator =
.
2) A string literal in c is surrounded by double quotes
3) Special characters such as the backslash ('\') in string literals have special meaning and must be escaped. 4) To do what you intend, you will need:
a) An additional counter to track the position in the line output
where the next line will be written
b) use strcpy/strncpy
instead of an assignment statement =
, so that each line in the switch statement will look something like this:
case 'f': strcpy(&output[j], "|="); j+=2; break;
Here j
- the second counter is incremented by the number of characters written onoutput
source to share
There are two problems in the code:
-
you are using
i
to iterate over the output array. The size of the input array and the output array are different. It may happen (for example in the casef
) that the index for the input array should be increased by 1, while the index for the output array should be increased by 2 or 3. -
you cannot assign multiple characters to a single char layer in an array. For example, it
case 'f': output[i]= '|='; break;
is wrong.
To solve the problem, you have to use another variable and increase 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;
source to share
Try to run
#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 kagark one by one, for example
case 'd': *q++ = '|'; *q++ = ')'; break;
source to share
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 single-quoted characters in C ++ are implementation-defined ( C ++ multicharacter literal ), you probably want to avoid them. Also, be careful when using "\" in literals (as with your last case), it may not do what you expect.
source to share