2012-12-06 47 views
1

我有f.e. 「我正在當護士。」 如何使用或使用哪種功能從字母1到空格或字母數字11獲取單詞?我怎樣才能從字符串

所以我應該得到「我的工作」

+0

你想要1個字或2個字? –

+0

C和C++是兩種不同的語言,尤其是涉及到字符串時;做出選擇。如果C++:http://www.cplusplus.com/reference/string/string/substr/,如果C:http://stackoverflow.com/questions/4214314/get-a-substring-of-a-char – AusCBloke

+0

看到'string :: substr'中。 – chris

回答

2

這是不完全清楚你想要什麼讀取流運營商使用一個詞>>,但是從你的榜樣好像你想要的字符串從字符1開始,然後結束於字符11個地方(總共12個字符)。這意味着你要string::substr

std::string str("I am working as a nurse"); 
std::string sub = str.substr(1,12); 
+0

我想他們想要的是第十一個角色,後面是10個角色。 – chris

+0

@chris他們給出的結果是「正在工作」,從字符1開始到字符12結束。實際上,這需要'substr(1,12)'。是的,這個問題沒有意義。 –

+0

哎呀,我以前算過11。你是對的。也許它應該完成它的話? – chris

6

要在一個字符串

std::stringstream stream("I am working as a nurse."); 
std::string word; 

stream >> word; // word now holds 'I' 
stream >> word; // word now holds 'am' 
stream >> word; // word now holds 'working' 
// .. etc 
0
char[] source = "I am working as a nurse." 
char[11] dest; 
strncpy(dest, &source[1], 10)