-4
나는 아래의 프로그램을 시도했다. 나는이문장에서 단어의 문자 수를 계산
string input = "hi everyone, what's up."
출력하려면 : 나는 문장에서 단어의 수를 계산 않았다
hi = 2
everyone = 8
whats= 5
up = 2
을하지만 문장에서 단어의 문자 수를 계산합니다.
나는 아래의 프로그램을 시도했다. 나는이문장에서 단어의 문자 수를 계산
string input = "hi everyone, what's up."
출력하려면 : 나는 문장에서 단어의 수를 계산 않았다
hi = 2
everyone = 8
whats= 5
up = 2
을하지만 문장에서 단어의 문자 수를 계산합니다.
Stackoverflow의 older queries을 (를) 다시 참조하십시오. 도움이 되셨을 것입니다!
다음#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str("Split me by whitespaces");
string buf; // Have a buffer string
stringstream ss(str); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
cout<< buf<<"="<<buf.length() <<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string s="hello there anupam";
int cnt,i,j;
for(i=0;s[i]!='\0';i++) /*Iterate from first character till last you get null character*/
{
cnt=0; /*make the counter zero everytime */
for(j=i;s[j]!=' '&&s[j]!='\0';j++) /*Iterate from ith character to next space character and print the character and keep a count of number of characters iterated */
{
cout<<s[j];
cnt++;
}
cout<<" = "<<cnt<<"\n"; /*print the counter */
if(s[j]=='\0') /*if reached the end of string break out */
break;
else
i=j; /*jump i to the next space character */
}
return 0;
}
은 당신이 원하는 무엇을의 작업 데모입니다. 나는 주석의 코드를 설명했다.
코드를 시도하십시오 –
는 2 개의 변수를 만듭니다. 1은 카운터이고 1은 currentWord 홀더입니다. currentWord 및 증분 카운터에 다음 문자를 추가하십시오. 공백, 쉼표, 점을 만나면 currentWord 및 카운터를 인쇄 한 다음 두 개를 모두 재설정하고 계속하십시오. –