Character replacement

I have 2 arrays

1
2
const char* chars1[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
const char* replace[] = {"26", "25", "24", "23", "22", "21", "20", "19", "18", "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"};


How can I make a program that loops through a string and if it finds the character ( b for example then it would replace it with the corresponding character in the replacement array( which would be 25 )
In C or C++? Forget replacing -- traverse the source string and strcat() or .append() the associated value into the destination string.

BTW, your mapping is not reversible.
could you give me some example code, I don't quite understand I'm pretty new
Anybody?
Let's break the problem down:

vypr11 wrote:
... make a program that loops through a string ...
Can you do that?

... if it finds the character ...
How would you check that?

... replace it with the corresponding character in the replacement array ...
How would you know what that is?
How would you replace a character?

Give it your best shot, and post it up here. We can start from there.
Last edited on
Your request is confused. You want to replace the character "a" with the character "26"? But "26" is two characters.

If you replace the letter z twice, it will look identical to replacing the letter p once.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const char* chars1[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
const char* replace[] = {"26", "25", "24", "23", "22", "21", "20", "19", "18", "17", "16", "15", "14", "13", "12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"};

std::string encode( std::string encrypt )
{
	std::string newstring;

	for(s = 0; s < encrypt.size(); s++)
	{
		if( encrypt[s] == "a"/*a value in the array, put letter a for testing*/)
		{
			newstring = encrypt[s] = "z";
			break();
		}
	}

	return newstring;
}


then do printf( encode("aaaa") ); basically what I am stuck on is making it check for a character in the array and then getting its corresponding value from the other array
Last edited on
Also @ moschops the array will soon be replaced with other characters( not 1 - 26) and each value will only be on character so its ok!
Do you know how to run a linear (or better maybe in this case is binary) search of an array for a target value?

Example (linear search results):
Find 'w' in "Hello, world!"
return 7 because "Hello, world!"[7] == 'w'

Find 'h' in "Hello, world!"
return -1 because "Hello, world!"[n] != 'h' for all 0 <= n < 13

Find 'o' in "Hello, world!"
return '4'
Last edited on
mathhead I have never seen c++ used in that Syntax, so I'm not quite sure how to do what you are saying
That's not C++, see http://en.wikipedia.org/wiki/Pseudocode

What I ment was do you know how to do that in C++? Something like:
1
2
3
4
string str = "Hello, world!";
cout << find('w', str) << ' ';
cout << find('h', str) << ' ';
cout << find('o', str) << endl;
7 -1 4
Oh, I know about psuedo code I just thought you were using real code! and know there is a find function is great however replacing is what should be difficult right? Unless I wanted to manually do it and make 26 find() lines
replacing is what should be difficult right?
1
2
3
string str = ...;
str[index] //to see one character
str[index] = '_' //to change a character 
1
2
3
4
5
6
7
int main()
{
  if( find('a', str)
  {
       str[a] = 'z'   
   }
}


something similar? Sorry I am very new, so thanks for sticking with me
http://cplusplus.com/reference/string/string/find

You call the function like this
1
2
3
4
5
6
string str, str2;
...
str.find('s'); //look for the char 's' in str 
str.find("something"); //look for the string "something" in str
str.find("s", 5); //look for the string "s" in str
str.find(str2); //look for str2 in str 


I made a program to help you understand how the find function works.
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
38

#include <iostream>
#include <string>

using namespace std;


int main() {
	string str;
	string target;
	string starting_index; //will be converted into an integer with atoi

	cout << "Enter a string to search: ";
	getline( cin, str );

	do {
		if( !cin.good() )
			return 0; //end the program if cin gets corrupted
    
		cout << "Enter a target to search for: ";
		getline( cin, target );

		cout << "Enter the index to start the search from (default is 0): ";
		getline( cin, starting_index );
		
		//search str for target starting at starting_index (parsed to an int)
		int index_found = str.find( target, atoi(starting_index.c_str()) );
		
		if( index_found < 0 )
			cout << '"' << target << "\" was not found in \"" << str;
		else
			cout << '"' << target << "\" was first found at " << index_found;
		cout << endl;
	} while( target != "exit" || starting_index == "exit" );

	return 0;
}
Topic archived. No new replies allowed.