strstr() mit array

Fat-Z

Mitglied
Hi Leute, ich habe hier ein PHP-Script das Zeichen, wie im Array angegeben, austauscht. Zum Beispiel: "a" wird zum "z", "b" wird zum "y" ...
Leider kann ich das nicht in C/C++ verwirklichen! :(
Kann mir da bitte jemand helfen und den PHP-Code in C/C++ übersetzen? Danke im Voraus!

PHP:
<?php

	function nicereplace ($str) {
		$chars = array(
						"a"=>"z", 
						"b"=>"y", 
						"c"=>"x",
						"d"=>"w",
						"e"=>"v",
						"f"=>"u"
		);

		$str = strtr($str, $chars);
		return $str;
	}
	
?>

Gruss << Fat-Z >>
 
Hallo,
da gibt es sicherlich mehrere Möglichkeiten, hier mal ein Beispiel:
C++:
#include <iostream>
#include <map>
#include <string>

std::string nicereplace(std::string str);

int main()
{
    std::cout << nicereplace("abcdef") << std::endl;
    return 0;
}

std::string nicereplace(std::string str)
{
    std::map<char, char> repl; // Ersetzungstabelle
    repl['a'] = 'z';
    repl['b'] = 'y';  
    repl['c'] = 'x'; 
    repl['d'] = 'w'; 
    repl['e'] = 'v'; 
    repl['f'] = 'u';

    std::string strResult = "";
    
    for( int i = 0; i < str.size(); i++ )
    {
        std::map<char, char>::iterator it = repl.find(str[i]);
        strResult += (it != repl.end() ? it->second : str[i]);
    }        

    return strResult;
}
Gruß
MCoder
 
Zurück