Replace two kind of char's arrays?

Hello dear programmers,

my question might be not very correct in the form of asking, but I got an making of doing a program that converts characters of one array into the another. I mean, replacing them, for example:


1
2
3
4
5
char a[]={'a','b','c'}
char b[]={'@','%','#'}

and here the code which replaces the a char into the b char, after a gets
as an input with cin


Is anything in this way possible?
Thanks a lot, I hope my question was not annoying.
Last edited on
For each letter in array a, go through the buffer looking for that letter. If found, the buffer takes on the value of whichever index a that is:
1
2
3
4
5
6
7
8
9
10
11
12
const char* buffer = "bcacab";
int buffer_len = sizeof(buffer) / sizeof(buffer[0]);

char a[]={'a','b','c', '\0'}
char b[]={'@','%','#', '\0'}
for (int i = 0; a[i]; i++)
{
  for (int j = 0; j < buffer_len; j++)
  {
    if (buffer[j] == a[i]) buffer[j] = b[i];
  }
}
Last edited on
Thanks a lot LowestOne, that's exactly what I was looking for! :)
Topic archived. No new replies allowed.