La solución para eliminar espacios de una cadena ya está publicada aquí . En este artículo otra solución usando flujo de cuerdas se discute.
eliminar el último compromiso git
Algoritmo:
1. Enter the whole string into stringstream. 2. Empty the string. 3. Extract word by word and concatenate to the string.
Programa 1: Usando EOF .
CPP// C++ program to remove spaces using stringstream #include using namespace std; // Function to remove spaces string removeSpaces(string str) { stringstream ss; string temp; // Storing the whole string // into string stream ss << str; // Making the string empty str = ''; // Running loop till end of stream while (!ss.eof()) { // Extracting word by word from stream ss >> temp; // Concatenating in the string to be // returned str = str + temp; } return str; } // Driver function int main() { // Sample Inputs string s = 'This is a test'; cout << removeSpaces(s) << endl; s = 'geeks for geeks'; cout << removeSpaces(s) << endl; s = 'geeks quiz is awesome!'; cout << removeSpaces(s) << endl; s = 'I love to code'; cout << removeSpaces(s) << endl; return 0; }
Producción
Thisisatest geeksforgeeks geeksquizisawesome! Ilovetocode
Complejidad del tiempo: O(n) donde n es la longitud de la cuerda
Espacio auxiliar: En)
Programa 2: Usando obtener línea() .
CPP
// C++ program to remove spaces using stringstream // and getline() #include using namespace std; // Function to remove spaces string removeSpaces(string str) { // Storing the whole string // into string stream stringstream ss(str); string temp; // Making the string empty str = ''; // Running loop till end of stream // and getting every word while (getline(ss temp ' ')) { // Concatenating in the string // to be returned str = str + temp; } return str; } // Driver function int main() { // Sample Inputs string s = 'This is a test'; cout << removeSpaces(s) << endl; s = 'geeks for geeks'; cout << removeSpaces(s) << endl; s = 'geeks quiz is awesome!'; cout << removeSpaces(s) << endl; s = 'I love to code'; cout << removeSpaces(s) << endl; return 0; } // Code contributed by saychakr13
Producción
Thisisatest geeksforgeeks geeksquizisawesome! Ilovetocode
Complejidad del tiempo: O (n)
Espacio auxiliar: O(n)
10ml en onzas