簡體   English   中英

Getline字符串輸入以創建單詞

[英]Getline string input to create a word

我有一個程序,該程序接收輸入並逐個字符地輸入字符,以免出現空格。 我現在需要做的是獲取不是空格的每個字符,並將它們作為單詞存儲在字符串中。

有人告訴我, getline存儲每個字符,它具有內存:

然后創建一個具有(!='')條件的while循環,然后使用string.append('x')函數將每個字符“添加”到您創建的字符串變量中,直到您有一個單詞為止。

我理解這個概念,但是我不知道該怎么做。

這是一個簡單的應用程序,它可以接收字符串並過濾出所有空格。

// reading a text file
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string input;
  stringstream filter;
  cout << "Enter a string \n";
  cin >> input;
  for(int i = 0; i<input.length(); i++){
      if(input.at(i)!=' '){ //Chech to see is it a space or not
          filter << input.at(i); //If not a space add to the stringstream filter
      }
  }
  string output = filter.str(); //Final string with no spaces

  return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM