如何分句 [英] how to split a sentence

查看:84
本文介绍了如何分句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C ++中将第一个单词拆分为字符串

例如.这是一个测试字符串.
这是一个例句.

我只想要结果为

这个

how to split the first word form the string in C++

Eg. This is a test string.
It is an example sentence.

I only want the result as

This
It

推荐答案

它非常简单.

让我们将字符串存储在数组中:

well it''s pretty simple.

let''s store the string in an array:

char str[20]="This is a test string";



并将结果存储在其他数组中:



and store the result in some other array:

char result[10];



然后使用for循环检测第一个空格"何时到达.第一个空格之前的字符串将是第一个单词.



then use a for loop to detect when does the first "space" comes. The string of characters which comes before the first space will be the first word.

for(i=0;str[i]!=" ";i++)
{
 result[i]=str[i];
}



当条件str [i]!="变为false时,for循环退出.
因此您的结果将存储在result中.

希望您了解它是如何工作的,这更重要!



the for loop exits when the condition str[i]!=" " becomes false.
so your result is stored in result.

Hope you understood how it worked which is more important!


尝试以下操作:

Try something as follows:

string str = "This is a test string";

int i = str.find_first_of(' ');

string res(str.begin(), str.begin() + i);


不确定C ++语法是,但是在大多数语言中,您将在空格处分割行并从结果数组中获取第一个元素.
not sure what the C++ syntax is, but in most languages you would split the line on the space and take the first element from the resultant array.


这篇关于如何分句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆