Veröffentlicht 20. Februar 200817 j es ist nur kleines Programm in C++. Es geht soweit, weiss jemand per Zufall wie ich das dritte Wort des C-Strings ausgeben kann? Die Ziffernfolge 0039049 wäre eigentlich nicht bekannt und das Programm muss solange suchen, bis es merkt das ein anderer Wert kommt als 20 (Leerzeichen), dass eine Ziffer kommt und dort muss es das dritte Wort (fett markiert) nehmen und ausgeben. Dev C++ #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { char str[] = " 0 AZ 0039049 001522300x"; char * pch; pch=strchr(str,' ');// NULL an letzter Stelle printf("Die Ziffernfolge 00122300 kommt an Stelle %d vor\n",pch-str+1); system("PAUSE"); return 0; }
20. Februar 200817 j Wenn es C++ sein soll, sollte man auch die Möglichkeiten von C++ nutzen: #include <sstream> #include <string> #include <iostream> using namespace std; int main() { string str = " 0 AZ 0039049 001522300x"; string s; istringstream stream( str ); stream >> s >> s >> s; string::size_type pos = str.find( s ); cout << "Die Ziffernfolge " << s << " kommt an Stelle " << pos << " vor\n"; }[/code]
20. Februar 200817 j Hej Klotzkopp, danke dir! hab noch diese Lösung erarbeitet. #include <vector> #include <string> #include <iostream> using namespace std; int main( ) { const char str[ ] = " 0 AZ 00-9049 001522300x"; std::string temp( str ); //std::string::size_type pos = temp.find( ' ', 0 ); //std::string::size_type old_pos = 0; std::vector< std::string > substrings; int pos = 0; int start_pos = 0; int end_pos = 0; bool found = false; while(str[pos] != 0) { if( (str[pos] > 47 && str[pos] < 58) || (str[pos] > 64 && str[pos] < 91)){ if(found){ end_pos = pos+1; } else{ found = true; start_pos = pos; end_pos = pos+1; } } else{ if(found){ substrings.push_back(temp.substr(start_pos, end_pos - start_pos)); start_pos = pos; end_pos = pos; found = false; } } pos++; //old_pos = ++pos; //pos = temp.find( ' ', pos ); //substrings.push_back( temp.substr( old_pos, pos - old_pos ) ); } for( std::vector< std::string >::size_type i = 0, size = substrings.size( ); i < size; ++i ) { std::cout << i + 1 << ": " << substrings[ i ] << std::endl; } system("PAUSE"); return 0; } Gib mir zwar alle Werte aus aber reicht. Deine iIdee ist auch super!
Archiv
Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.