example to split a cpp string into tokens

Discussion in 'C++' started by rustyoldguy, Mar 8, 2020.

  1. rustyoldguy

    rustyoldguy New Member

    Joined:
    Feb 21, 2020
    Messages:
    3
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Male
    Location:
    Germany
    Hi!

    Here i wrot a little program to split a cpp string into tokens without getline.
    The several tokens will save in a vector of cpp strings. After this done
    the several vectors are converted into integer values

    first header strwork.h:
    Code:
    #ifndef STRWORK_INC_
    #define STRWORK_INC_ 1
    
    #include <iostream>  
    #include <stdlib.h> //atoi EXIT_SUCCESS
    #include <string.h> // strcpy, strtok
    #include <vector>
    #include <sstream> // stringstream
    #include <complex.h>
    
    
    std::string showchar(int nummer);
    int StrToInt(std::string zawo);
    int is_allowed(std::string mot, std::string erlaubt);
    int showvector(std::vector<std::string> tokens);
    
    
    #endif
    
    now strwork.cpp:
    Code:
    #include "strwork.h"
    
    std::string showchar(int nummer) // old DOS 437-font charctes 166 and 167 are of font 851, beceause characters of font 437 not showable
    {                        //128                                                                   142            145  146  147
                             //148  149  150                      155                      160                      165
     int i = 0;              // 0    1    2    3    4    5    6    7    8    9    10  11   12    13   14   15   16  17   18   19
     std::string gletter = "";
     std::string fremde[128]= {"Ç", "ü", "é", "â", "ä", "à", "å", "ç", "ê", "ë", "è", "ï", "î", "ì", "Ä", "Å", "É", "æ", "Æ", "ô",  // 128 bis 147 0-19
                               "ö", "ò", "û", "ù", "ÿ", "Ö", "Ü", "ḉ", "£", "¥", "₧", "ƒ", "á", "í", "ó", "ú", "ñ", "Ñ", "Ğ", "ğ",  // 148 bis 167 20-39
                               "¿", "┌", "¬", "½", "¼", "¡", "«", "»", "░", "▒", "▓", "│", "┤", "╡", "╢", "╖", "╕", "╣", "║", "╗",  // 168 bis 187 40-59   
                               "╝", "╜", "╛", "┐", "└", "┴", "┬", "Ⱶ", "─", "┼", "╞", "╟", "╚", "╔", "╩", "╦", "╠", "═", "╬", "╧",  // 188 bis 207 60-79
                               "╨", "╤", "╥", "╙", "╘", "╒", "╓", "╫", "╪", "┘", "┌", "▉", "▄", "▌", "▐", "▀", "⍺", "ß", "Γ", "╥",  // 208 bis 227 80-99
                               "Ʃ", "σ", "µ", " ", "⧲", "Ɵ", "Ω", "δ", "ꝏ", "ø", "Ꞓ", "Ո", "≡", "±", "≥", "≤", "∫", "∫", "÷", "≈", // 228 bis 247 100-119
                              "°", "·", "·", "√", "ⁿ", "²", "▖", ""};  // 248 bis 255 120-127
     if (nummer >= 128)
      i = nummer - 128;
       else
       {
         if (nummer != 127) gletter += (char)nummer;  // length of cpp-string is zero add ASCII-characters of array fremde
         if (nummer == 127) gletter =  " ⃤ ";
        return gletter;   
       }
       
    //std::cout << "*" << i << "* ";
    
    if ((i >= 0) && (i < 128)){
     //std::cout << "*" << i << "* ⃤ ";
       return fremde[i];
    }
     
     return fremde[127];
    }
    
    
    // converts a cpp-string in to a int-value
    int StrToInt(std::string zawo)
    {
     int rewer = 0;
     std::istringstream b(zawo);
     b >> rewer;
     return rewer;
    }
    
    // check if all characters of 'mot'(arg1) exists in string 'erlaubt'
    // if yes return value = 0, otherwise -1  
    int is_allowed(std::string mot, std::string erlaubt)
    {
     int i, zaehler = 0, molen = -1; // german: zaehler----> english counter
     std::size_t found;
     std::string str2 = "";
     std::string charmot = "";       
     
      molen = mot.length();
    
     for (i = 0; i < molen; i++)   
      {
       // splits cpp-string 'erlaubt' into characters
      charmot = mot.substr(i, 1);
      found = erlaubt.find(charmot);
      //std::cout << "charmot=" << charmot << "    found=" <<  found << std::endl;
      if (found != std::string::npos) // Wenn teil gefunden wurde
       {
       //std::cout << "\nzehler=" << zaehler << std::endl;
          zaehler++;     
        }
      }
      //std::cout << "*mot=" << mot << " erlaubt["<< i << "]= "<< erlaubt[i] <<  "  zaehler: " << zaehler << " molen=" << molen << std::endl;   
       
     if (zaehler == molen) zaehler = 0;
      else zaehler = -1;  
    
     return zaehler;
    }
    
    
    int showvector(std::vector<std::string> tokens)
    {
     int i, lge = tokens.size();
    
     std::cout << "\nshow vector" << std::endl;
     std::cout << "vector laenge" << lge << std::endl;
     // Printing the token vector
     for(i = 0; i < lge; i++)
     std::cout << "i=" << i <<  "  "  << tokens[i] << '\n';
    
     return lge;   
    }
    
    
    at last main.cpp:
    Code:
    
    // splits a-cpp String into several parts
    
    #include "strwork.h"
    
    void splitcppstr(std::string tosplit, std::string delimiter, std::vector<std::string> &tokens)
    {
     int lge = -1, partlen = 0, posa = -1;
     std::size_t found;  // wegen find
     std::string str2 = "";
     
     lge = tosplit.length();
     
     found = tosplit.find(delimiter);
     if (found != std::string::npos) // Wenn teil gefunden wurde
      {
       partlen = found;
       str2=tosplit.substr(0, partlen);
       tokens.push_back(str2);  
       ///std::cout << "delimiter=" << delimiter << "  an pos "<< found << "   " << str2 <<  std::endl;
      }
    
     while(found != std::string::npos)   
      {
       found++;
       posa = found;
       found = tosplit.find(delimiter, found);
      //std::cout << "charmot=" << charmot << "    found=" <<  found << std::endl;
     
      if (found != std::string::npos) // Wenn teil gefunden wurde
       {
       partlen = found - posa;
       str2=tosplit.substr(posa, partlen);
       tokens.push_back(str2);  
       ///std::cout << "delimiter=" << delimiter << "  an pos " << found << "   " << str2 <<  std::endl;
       }
      }
     
      if (posa != -1)
       {
        partlen = lge - posa + 1;
        str2=tosplit.substr(posa, partlen);
       tokens.push_back(str2);
        ///std::cout <<"str2= " << str2 <<  " posa="<< posa << std::endl;   
       }   
     
    }
       
    int main(int argc, char **argv)
    {
     std::string isterlaubt = ".0123456789"; // ist erlaubt --> german for is allowed
     std::string zeile[2] = {"78 101 118 101 114 32 100 114 105 110 107 32 109 111 114 101 32 97 115 32 50 49 32 98 101 101 114 101",
                             "68 114 105 110 107 32 98 101 116 116 101 114 32 49 53 33"};
     std::string delimiter = " ";
     // Vector of string to save tokens
     std::vector<std::string> tokens;
     int i, j, lge, fehler = 0;
     int numbers[30] = {0};
       
     for (j = 0; j < 2; j++)
     {
      if (j) tokens.clear();
       
     
       std::cout << "\n\nString with integer numbers to split into tokens:" << std::endl << zeile[j] << std::endl;
       std::cout << "0123456789012345678901234567890" << std::endl << std::endl;
       splitcppstr(zeile[j], delimiter, tokens);
             
       lge = showvector(tokens);
     
       std::cout <<"\n---------------------------"<< std::endl;
       
      std::cout <<"length of vector: " << lge << "\n\nnumbers as text: " << std::endl;
      for(i = 0; i < lge; i++)
       {
       fehler = is_allowed(tokens[i], isterlaubt);
       if (fehler == 0)
           {
            numbers[i] = StrToInt(tokens[i]);
           //std::cout << "ival[" <<i << "]=" << numbers[i] << std::endl; //"   fehler=" << fehler << std::endl;
           std::cout << showchar(numbers[i]);
          }
          else
            {
             std::istringstream(tokens[i]) >> std::hex >> numbers[i];
            std::cout << showchar(numbers[i]);
           }     
       }  
     } // ende j-Schleife
     std::cout <<"\n\nEnd of program"<< std::endl;
    
    return EXIT_SUCCESS;
    }
    
    
    

    I wrote
    std::string showchar(int nummer)
    to convert old files where written with MSDOS to files for Linux.

    But i hope, it can be used by people with Windows

    Have much fun
    rustyoldguy
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice