• [C++] character replacement program
    9 replies, posted
I want to make a program that takes an input string and replaces characters however I have no idea how to get it started [code] format is: original char -> char i want it to be replaced with a -> z | A -> Z b -> y | B -> Y c -> x | C -> X d -> w | D -> W e -> v | E -> V f -> u | F -> U g -> t | G -> T h -> s | H -> S i -> r | I -> R j -> q | J -> Q k -> p | K -> P l -> o | L -> O m -> n | M -> N n -> m | N -> M o -> l | O -> L p -> k | P -> K q -> j | Q -> J r -> i | R -> I s -> h | S -> H t -> g | T -> G u -> f | U -> F v -> e | V -> E w -> d | W -> D x -> c | X -> C y -> b | Y -> B z -> a | Z -> A [/code] Any one know how I could start this? I was thinking something along the lines of a multi dimensional array to store the characters, however i do not know how to search the string for character a and replace it with character ( z for example ) I haven't done very much in c++ so I appreciate you guys helping me out
[url]http://www.cplusplus.com/reference/string/string/replace/[/url] Something like that? Sorry for not begin more constructive. I just saw the thread on my way to bed.
[QUOTE=Freze;29954536][url]http://www.cplusplus.com/reference/string/string/replace/[/url] Something like that? Sorry for not begin more constructive. I just saw the thread on my way to bed.[/QUOTE] Possibly, I'm not quite sure on how to use that though( I know in lua you would just do string.Replace(strString1, "a", "z"); )
You can use the fact that chars are actually just ints in disguise. For your purposes A-Z are actually the numbers 65-90 a-z are actually the numbers 97-122 anything else doesn't get parsed or just stays the same You can use this fact to create an array of conversions. So it'd be something like: [cpp] conversion[0] = 'Z' conversion[1] = 'Y' ... conversion[25] = 'A' conversion[26] = 'z' ... conversion[51] = 'a' [/cpp] Depending on the case of the character you input, access the array by [cpp] conversion[inputChar - 65] // if upper case conversion[inputChar - 71] // if lower case [/cpp] So you go through the input string (a string is actually an array of chars) char-by-char and check the character is valid first of all, if it is, then retrieve its partner from the conversion array and print it (or do whatever you want to do with it) otherwise ignore it. That's just if you wanted to do A-Z,a-z. If you wanted to define replaces for the whole ASCII set of characters, then create a char array of size 128 and simply access the converted character by [cpp] conversion[inputChar] [/cpp] Hope that made some sense!
You could just check the case and add the distance from you character to z/Z to a/A. [code]for each char c in string if(c > 'a' && c < 'z') new_string += 'a' + 'z' - c else if(c > 'A' && c < 'Z') new_string += 'A' + 'Z' - c else new_string += c //?[/code] This is however quite specialized. If you wanted a more general case of replacing something with something else and you can't use easy maths, then you'd probably use an array of sorts with a std::pair and use it like [code]for each element_pair in array while((pos = string.find(element.first) is valid position) string.replace(pos, element.second.length, element.second)[/code]
Funnily enough, I have a program that does this that I wrote agesss ago. Surprised it even works, honestly. [cpp]#include <iostream> #include <string> #include <sstream> #include <cctype> using namespace std; std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; std::string encode(std::string toEncode) { std::stringstream encoded; for(unsigned int j = 0; j < toEncode.size()+1; j++) { for(unsigned int i = 0; i < alphabet.size()+1; i++) { if(toEncode[j-1] == alphabet[i]) { encoded << alphabet[alphabet.size() - j]; break; } } } return encoded.str(); } std::string decode(std::string toDecode) { std::stringstream decoded; for(unsigned int j = 0; j < toDecode.size()+1; j++) { for(unsigned int i = 0; i < alphabet.size()+1; i++) { if(toDecode[j+1] == alphabet[i]) { decoded << alphabet[j]; break; } } } return decoded.str(); } int main() { cout << encode("abcdefghijklmnopqrstuvwxyz") << endl; cout << decode("abcdefghijklmnopqrstuvwxyz"); return 0; } [/cpp] It doesn't handle caps, though. [editline]21st May 2011[/editline] Output is: [code]zyxwvutsrqponmlkjihgfedcba abcdefghijklmnopqrstuvwxyz[/code] which I believe is correct. [editline]21st May 2011[/editline] Wait, I just realised the decode function doesn't actually do anything. Eh. Whatever.
[QUOTE=BlkDucky;29955118]Funnily enough, I have a program that does this that I wrote agesss ago. Surprised it even works, honestly. [cpp]#include <iostream> #include <string> #include <sstream> #include <cctype> using namespace std; std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; std::string encode(std::string toEncode) { std::stringstream encoded; for(unsigned int j = 0; j < toEncode.size()+1; j++) { for(unsigned int i = 0; i < alphabet.size()+1; i++) { if(toEncode[j-1] == alphabet[i]) { encoded << alphabet[alphabet.size() - j]; break; } } } return encoded.str(); } std::string decode(std::string toDecode) { std::stringstream decoded; for(unsigned int j = 0; j < toDecode.size()+1; j++) { for(unsigned int i = 0; i < alphabet.size()+1; i++) { if(toDecode[j+1] == alphabet[i]) { decoded << alphabet[j]; break; } } } return decoded.str(); } int main() { cout << encode("abcdefghijklmnopqrstuvwxyz") << endl; cout << decode("abcdefghijklmnopqrstuvwxyz"); return 0; } [/cpp] It doesn't handle caps, though. [editline]21st May 2011[/editline] Output is: [code]zyxwvutsrqponmlkjihgfedcba abcdefghijklmnopqrstuvwxyz[/code] which I believe is correct. [editline]21st May 2011[/editline] Wait, I just realised the decode function doesn't actually do anything. Eh. Whatever.[/QUOTE] Oh wow! I got back to my computer and had all this help, just curious, why do you say the decode function doesn't do anything? [editline]21st May 2011[/editline] Oh you mean because you are giving it a string that is already decoded
[cpp]#include <stdlib.h> char switchCases(char letter) { if(isupper(letter)) { return tolower(letter); } else { return toupper(letter); } }[/cpp]
[QUOTE=BlkDucky;29955118] [cpp]#include <iostream> #include <string> #include <sstream> #include <cctype> using namespace std; std::string alphabet = "abcdefghijklmnopqrstuvwxyz"; std::string encode(std::string toEncode) { std::stringstream encoded; for(unsigned int j = 0; j < toEncode.size()+1; j++) { for(unsigned int i = 0; i < alphabet.size()+1; i++) { if(toEncode[j-1] == alphabet[i]) { encoded << alphabet[alphabet.size() - j]; break; } } } return encoded.str(); } std::string decode(std::string toDecode) { std::stringstream decoded; for(unsigned int j = 0; j < toDecode.size()+1; j++) { for(unsigned int i = 0; i < alphabet.size()+1; i++) { if(toDecode[j+1] == alphabet[i]) { decoded << alphabet[j]; break; } } } return decoded.str(); } int main() { cout << encode("abcdefghijklmnopqrstuvwxyz") << endl; cout << decode("abcdefghijklmnopqrstuvwxyz"); return 0; } [/cpp] [/QUOTE] Why use namespace std and still put std:: infront of all the strings?
[QUOTE=The DooD;29993062]Why use namespace std and still put std:: infront of all the strings?[/QUOTE] Because I'm very inconsistent. :v: But really, C::B generated the base file which was: [cpp] #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; return 0; }[/cpp] Or close to that. And I edited that file rather than starting over. I tend to put std:: rather than use the namespace.
Sorry, you need to Log In to post a reply to this thread.