hallo brauche hilfe bei Zeichen wechseln C++



  • oder die reine c variante

    void replacechar(char* string, const char a, const char b)
    {
        char* temp = string;
    
        if (!string) return;
        while (*temp) {
            if (*temp == a) *temp = b;
            temp++;
        }
        return;
    }
    


  • oder als 1-Zeiler:

    void Mod_string(char* string, char source, char dest)
    {
    	for (int i(0);i<strlen(string);++i) if (string[i]==source) string[i]=dest;
    }
    


  • _matze schrieb:

    Schönes Beispiel! Das kann er niemals beim Lehrer angeben. Der glaubt ihm nie im Leben, das selbst gemacht zu haben.

    Ich glaub darum ging es in der Antwort.

    Nebenbei war die Aufgabenstellung übrigens, das in einer Funktion zu erledigen.

    Dann eben

    void Mod_string(char* string, char source, char dest)
    {
    	class replace_char
    	{
    	public:
    		replace_char(const char src, const char dest)
    		: source(src), destination(dest)
    		{ }
    
    		void operator()(char& aChar)
    		{
    		  if (aChar == source) {
    			 aChar = destination;
    		  }
    		}
    	private:
    		const char destination;
    		const char source;
    	};
    
       int length = strlen(string);
       std::for_each(string, string+length, replace_char(source, dest));
    }
    


  • DerKuchen schrieb:

    _matze schrieb:

    Schönes Beispiel! Das kann er niemals beim Lehrer angeben. Der glaubt ihm nie im Leben, das selbst gemacht zu haben.

    Ich glaub darum ging es in der Antwort.

    Das war mir schon klar. Ich wollte dem auch beipflichten! Da vergisst man ein mal die blöden Smileys... 😉



  • Warum sich mit for_each abmühen?

    void Mod_String( std::string& s, char source, char dest )
    {
        std::replace( s.begin(), s.end(), source, dest );
    }
    


  • DerKuchen schrieb:

    Dann eben

    void Mod_string(char* string, char source, char dest)
    {
    	class replace_char
    	{
    	public:
    		replace_char(const char src, const char dest)
    		: source(src), destination(dest)
    		{ }
    
    		void operator()(char& aChar)
    		{
    		  if (aChar == source) {
    			 aChar = destination;
    		  }
    		}
    	private:
    		const char destination;
    		const char source;
    	};
    
       int length = strlen(string);
       std::for_each(string, string+length, replace_char(source, dest));
    }
    

    ISO/IEC 14882:2003 schrieb:

    14.3.1 §2
    A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.



  • Tachyon schrieb:

    ISO/IEC 14882:2003 schrieb:

    14.3.1 §2
    A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

    Und wenn ich Bitte sage?

    Dann muss man eben doch die langweilige Version mit std::replace nehmen *seufz*



  • DerKuchen schrieb:

    Dann muss man eben doch die langweilige Version mit std::replace nehmen *seufz*

    Kannst sie ja ein bißchen aufbohren:

    replace_if( s.begin(), s.end(), bind1st( equal_to<char>(), source ), dest );
    


  • void replace(char* lll, char ll1, char l11){
      do *lll = *lll == ll1 ? l11 : *lll; while (*(lll++));
    }
    


  • Fellhuhn schrieb:

    void replace(char* lll, char ll1, char l11){
      do *lll = *lll == ll1 ? l11 : *lll; while (*(lll++));
    }
    

    Hehe, und das am Besten noch als Makro...


  • Mod

    void replace(char* i, char í, char ì){
      while((*i^í||(*i^=í^ì))&&*i++);
    }
    


  • omg, jetzt gehts los



  • Da wir alle etwas um die Performance besorgt sind, ein wenig Loop Unrolling:

    void Mod_string(char *str, char src, char dest)
    {
    	int n = (strlen(str) + 7) / 8;
    
    	switch (n % 8) {
    		case 0: do { *str = *str == src ? dest : *str; str++; 
    		case 7:      *str = *str == src ? dest : *str; str++;
    		case 6:      *str = *str == src ? dest : *str; str++;
    		case 5:      *str = *str == src ? dest : *str; str++;
    		case 4:      *str = *str == src ? dest : *str; str++;
    		case 3:      *str = *str == src ? dest : *str; str++;
    		case 2:      *str = *str == src ? dest : *str; str++;
    		case 1:      *str = *str == src ? dest : *str; str++;
    				} while (--n > 0);
    	}
    }
    

Anmelden zum Antworten