Resource icon

Deutsche Umlaute in cpp und Linux mit tolower-Ersatzu ersetzen

Programmiersprache(n)
c++
Betriebssystem(e)
Linux
Hallo Leute!

Wer von euch ist in c++ schon über die deutschen Umlaute bei dem Versuch gestolpert,
Großbuchstaben in Kleinbuchstaben umzuwandeln? Nach einiger Bastelei hier ein
kleines Programmbeispiel das ich in Zukunft versuchen werde, zu erweitern.

Eigentlich könnte man so vorgehen:
Code:
#include <iostream>
#include <algorithm>

std::string str_tolower(std::string s) {
    std::transform(s.begin(), s.end(), s.begin(),
                // static_cast<int(*)(int)>(std::tolower)           // wrong
                // [](int c){ return std::tolower(c); }                   // wrong
                // [](char c){ return std::tolower(c); }                // wrong
                   [](unsigned char c){ return std::tolower(c); } // correct
                  );
    return s;
}


int main()
{
    std::string s("Teststring ÖÄÜ test");
    std::transform(s.begin(), s.end(), s.begin(), (int (*)(int))std::toupper);
    std::cout << s;

return 0;
}
Bei dem Beispiel, welches ich im Web gefunden habe, wird man aber merken, das dies die deutschen Umlaute kalt lässt

Code:
/** Testet eigenen tolower-Ersatz: er schluckt die deutschen Umlaute und die Sonderzeichen */
#include <iostream>
#include <string>


std::string de_tolower(std::string upsatz)
{
std::string losatz = "";
std::string gxe[29] = {"Ö", "Ä", "Ü", "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"};
std::string kxe[29] = {"ö", "ä", "ü", "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"};

int slei = 0, slen = 0,  p = 1;

for (slei = 0; slei < 29; slei++)
{
p = upsatz.find(gxe[slei]);
  while(p != -1)
   {
    //std::cout << "p: "   << p << " *   ";
    //std::cout << upsatz << "  gxe: " << gxe[slei] << "   p: " << p << "     n: ";
     slen = gxe[slei].length();
     upsatz.erase(p,slen);
     upsatz.insert(p, kxe[slei]);
     //std::cout << upsatz << std::endl;
     p = upsatz.find(gxe[slei]);
    }
}

return upsatz;
}

std::string de_toupper (std::string upsatz)
{
std::string losatz = "";
std::string kxe[29] = {"Ö", "Ä", "Ü", "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"};
std::string gxe[29] = {"ö", "ä", "ü", "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"};

int slei = 0, slen = 0,  p = 1;

for (slei = 0; slei < 29; slei++)
{
p = upsatz.find(gxe[slei]);
  while(p != -1)
   {
    //std::cout << "p: "   << p << " *   ";
    //std::cout << upsatz << "  gxe: " << gxe[slei] << "   p: " << p << "     n: ";
     slen = gxe[slei].length();
     upsatz.erase(p,slen);
     upsatz.insert(p, kxe[slei]);
     //std::cout << upsatz << std::endl;
     p = upsatz.find(gxe[slei]);
    }
}

return upsatz;
}

int main()
{
std::string stria = "Teststring ÖÄÜß²³µ€@~ test";
std::string strib = "";
std::string stric = "";

std::cout << "vorher........: " << stria << std::endl;

strib = de_tolower(stria);

std::cout << "nachher.......: " << strib << std::endl;

stric = de_toupper(strib);

std::cout << "wieder nachher: "<< stric << std::endl;

return EXIT_SUCCESS;
}
Gecodet wurde auf SUSE Linux tumbleweed mit Kernel 4.16.12.2
und codeblocks build 2017-07-27 mit gcc 7-2.9

Viel Spaß
rustyoldguy
Autor
rustyoldguy
First release
Last update
Bewertung
0,00 Stern(e) 0 Bewertungen
Oben