Home > Back-end >  My algorithm for replacing characters in string is not working
My algorithm for replacing characters in string is not working

Time:09-12

For example when I input in the terminal

hello there

it prints

gmllg mgmkm

instead of printing

itssg zitkt

which is not supposed to happen.

Another example is when I input in the terminal

abcdefghijklmnopqrstuvwxyz

it prints

jvmkmnbgghaldfghjklmbcvbnm

the last 5 characters are right but its supposed to print

qwertyuiopasdfghjklzxcvbnm

Anyways I can fix this?

Here is the code below

#include <cs50.h>
#include <stdio.h>
#include <string.h>
void replace_char(string text, string replace, string new);
int main(void){

    string message = get_string("Type message: ");
    string Alpha = "abcdefghijklmnopqrstuvwxyz";
    string Key  =  "qwertyuiopasdfghjklzxcvbnm";
    replace_char(message, Alpha, Key);
    printf("%s\n", message);
}
void replace_char(string text, string replace, string new){

    int strl = strlen(text);
    int h;
    int p;
    for (h = 0; h < strl; h  ){
        for (p = 0; p < 26; p  )
        if (text[h] == replace[p])
        text[h] = new[p];}
}

CodePudding user response:

The problem is the inner for loop

for (h = 0; h < strl; h  ){
    for (p = 0; p < 26; p  )
    if (text[h] == replace[p])
    text[h] = new[p];}

For example if text[h] is equal to replace[p] then the character text[h] is changed and the inner loop continues its iterations with the new value of text[h] And the new value of text[h] can be again found in the string replace.

At least you need to insert a break statement

for (h = 0; h < strl; h  ){
    for (p = 0; p < 26; p  )
    { 
        if (text[h] == replace[p])
        {
            text[h] = new[p];
            break;
        }
    }
}

You could use the standard function strchr instead of writing manually the inner for loop. For example

string replace_char( string text, string replace, string new )
{
    for ( string p = text; *p != '\0';   p )
    {
        char *target = strchr( replace, *p );
        if ( target != NULL ) *p = new[target - replace];
    }

    return text;
}

Pay attention to that you need to guarantee that lengths of the strings replace and new are equal each other. Otherwise the function will look more complicated.

CodePudding user response:

The problem is that after you've replaced the character you continue searching and may therefore replace the character you just put there. This may happen multiple times. When you find a match, you need to break out of the loop:

void replace_char(string text, string replace, string new){
    int strl = strlen(text);

    for (int h = 0; h < strl; h  ){
        for (p = 0; p < 26; p  ) {
            if (text[h] == replace[p]) {
                text[h] = new[p];
                break;                   // break out
            }
        }
    }
}

An even simpler version would be to just look up the character in the replacement string directly. This works if the characters in replace are in a contiguous range (and they are 26 of them as you've hardcoded):

void replace_char(string text, string replace, string new){
    int strl = strlen(text);

    for (int h = 0; h < strl; h  ){
        if(text[h] >= replace[0] && text[h] <= replace[25])
            text[h] = new[text[h] - replace[0]];
    }
}
  • Related