我如何...显示具有特定单词的字符串 [英] How do I...Display string which has particular word

查看:88
本文介绍了我如何...显示具有特定单词的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我想在vc ++中显示字符串,其中包含特定的单词。



例如



String 1 Wcout<<这是tom<< endl;





String 2 Wcout<< ;tom and jerry是一个卡通节目<< endl;



String 3 Wcout<<我是BS计算机科学专业学生<< ; endl;



字符串4 Wcout<<Tom cruise是我最喜欢的演员<< endl;



字符串5 Wcout<<C ++是一种面向对象的语言<< endl;







现在我想要输出中的字符串,其中包含单词tom ..所以我想要string1,string2和string4作为我的输出..我应该使用find函数。我应该使用哪个参数。或者是否有更好的方法来实现这样的逻辑。

hi i want to display string in vc++ which has particular word in it.

for example

String 1 Wcout <<"this is tom" <<endl;


String 2 Wcout <<"tom and jerry is a cartoon show" <<endl;

String 3 Wcout <<"i am BS Computer science student" <<endl;

String 4 Wcout <<"Tom cruise is my favorite actor" <<endl;

String 5 Wcout <<"C++ is a object-oriented language" <<endl;



now i want string in my output which has word tom in it..so i want string1, string2 and string4 as my output..should i use find function for it. with which parameter i should use it. or is there any better way to implement such a logic.

推荐答案

根据要求设定的可能解决方案可能是:



A possible solution according to the requirements set could be:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

#define STRING_ARRAY_LENGTH 5

bool findString(std::string original, std::string search)
{
	/*
		this function tries to find a string inside another string.

		original - string with text to be searched.
		search - string with text to be found.

		returns true if 'search' exists in 'original'

		note: this function is not case sensitive.
	*/

	std::string::size_type foundPosition;

	std::transform(original.begin(), original.end(), original.begin(), ::tolower);
	std::transform(search.begin(), search.end(), search.begin(), ::tolower);

	foundPosition = original.find(search);

	return foundPosition != std::string::npos;
}

int main(void)
{
	std::string* myStringCollection = new std::string[STRING_ARRAY_LENGTH]
	{
		"this is tom",
		"tom and jerry is a cartoon show",
		"i am BS Computer science student",
		"Tom cruise is my favorite actor",
		"C++ is a object-oriented language"
	};

	int myStringIndex = 0;

	std::string mySearchString("tom");

	while(myStringIndex < STRING_ARRAY_LENGTH)
	{
		if ( findString(myStringCollection[myStringIndex], mySearchString) )
			std::cout << myStringCollection[myStringIndex] << std::endl;

		myStringIndex++;
	}

	return 0;
}





注意:这个例子是用gcc编译的。



JAFC



NOTE: This example was compiled in gcc.

JAFC


这篇关于我如何...显示具有特定单词的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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