Untitled
unknown
c_cpp
a year ago
2.0 kB
2
Indexable
Never
//#define _CRT_SECURE_NO_WARNINGS //#define MAX_STRING_LEN 1000 // //#include <string> //#include <iostream> //#include <vector> //#include <Windows.h> // //using namespace std; // //void get_string(char* str) //{ // fgets(str, MAX_STRING_LEN, stdin); // str[strcspn(str, "\n")] = '\0'; //} // // //int main() //{ // SetConsoleCP(1251); // SetConsoleOutputCP(1251); // // cout << "Введите исходную строку: "; // // char* words = new char[MAX_STRING_LEN]; // get_string(words); // // char* new_words = new char[MAX_STRING_LEN]; // new_words[0] = '\0'; // // char* word = strtok(words, " "); // char* endptr; // while (word != NULL) // { // double x = strtod(word, &endptr); // if (endptr == word) // { // new_words = strcat(new_words, word); // new_words = strcat(new_words, " "); // } // word = strtok(NULL, " "); // } // // // cout << new_words; // // delete[] new_words; // delete[] words; //} // // //#include <iostream> #include <string> #include <iostream> #include <Windows.h> using namespace std; int word_end_index(string words, int start) { int a = words.find(" ", start) - 1; if (a < 0) return words.size() - 1; return a; } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); cout << "Введите исходную строку: "; string words, new_words; getline(cin, words); for (int word_start = 0; word_start < words.size(); word_start++) { if (words[word_start] == ' ') continue; int word_end = word_end_index(words, word_start); int word_len = word_end - word_start + 1; string word = words.substr(word_start, word_len); char* endptr; double x = strtod(word.c_str(), &endptr); if (endptr != word.c_str() + word_len) { new_words += word + " "; } word_start = word_end; } cout << new_words; }