compare one character with an array of characters

Q. How do I compare one character with an array of characters ???

for example I have an array of characters array[100]; or a string; now I want to compare a character say "a" with each character in that array or string and replace it with something else. e.g I want to replace all the a's in the array with b's. How do I do this.

below example doesn't seem to work :(

1
2
3
4
for(i=0;i<100;i++)
{
 if (arr[i]=="a") arr[i]="b";
}
below example doesn't seem to work :(
yes, and it should not. You are comparing and trying to assign string to character.

simpliest way:
1
2
3
#include <algorithm>
//...
std::replace(arr, arr + 100, 'a', 'b');
What exactly did you do here? What does 'replace' constitute?
replace means if array has following characters abcdafeh, i want to replace all the a's so it will become bbcdbfeh
@MiiNiPaa thanks man :D
another question, I want to do it with the entire alphabet, but I don't want to replace the letters that are already replaced :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    std::replace(result, result + 100, 'a', 'l');
    std::replace(result, result + 100, 'b', 'j');
    std::replace(result, result + 100, 'c', '6');
    std::replace(result, result + 100, 'd', 'h');
    std::replace(result, result + 100, 'e', 's');
    std::replace(result, result + 100, 'f', '8');
    std::replace(result, result + 100, 'g', 'k');
    std::replace(result, result + 100, 'h', 'c');
    std::replace(result, result + 100, 'i', 'o');
    std::replace(result, result + 100, 'j', '2');
    std::replace(result, result + 100, 'k', 'a');
    std::replace(result, result + 100, 'l', 't');
    std::replace(result, result + 100, 'm', '7');
    std::replace(result, result + 100, 'n', 'g');
    std::replace(result, result + 100, 'o', ' ');
    std::replace(result, result + 100, 'p', 'w');
    std::replace(result, result + 100, 'q', 'e');
    std::replace(result, result + 100, 'r', 'm');
    std::replace(result, result + 100, 's', 'p');
    std::replace(result, result + 100, 't', '1');
    std::replace(result, result + 100, 'u', '4');
    std::replace(result, result + 100, 'v', 'f');
    std::replace(result, result + 100, 'w', '9');
    std::replace(result, result + 100, 'x', 'y');
    std::replace(result, result + 100, 'y', '0');
    std::replace(result, result + 100, 'z', '5');
    std::replace(result, result + 100, '1', 'r');
    std::replace(result, result + 100, '2', 'v');
    std::replace(result, result + 100, '3', 'd');
    std::replace(result, result + 100, '4', 'n');
    std::replace(result, result + 100, '5', 'i');
    std::replace(result, result + 100, '6', 'j');
    std::replace(result, result + 100, '7', 'u');
    std::replace(result, result + 100, '8', 'x');
    std::replace(result, result + 100, '9', 'b');
    std::replace(result, result + 100, '0', 'q');
    std::replace(result, result + 100, ' ', '3');

here a converts into l, l converts into t, t converts into 1, and 1 again converts into v, I only want to convert the characters only once.
Then you will need to setup loop and replace characters manually.
Topic archived. No new replies allowed.