Moving in string

Hey i want to make program which search in string if there is same word ex. "This word is the same word ". And it should outpout this string with changed console color when it encounters the same word . And is there any function or anything to compare them like that ? Because i know how to compare previous word but idk how to do it for my program.
Sry for my english.
Last edited on
but idk how to do it for my program.


What program, have you written code? show it to us. Add add it between code tags - http://www.cplusplus.com/articles/jEywvCM9/
Read : "Hey i want to make program which search in string if there is same word ex. "This word is the same word ". And it should outpout this string with changed console color when it encounters the same word . And is there any function or anything to compare them like that ?" Btw no i haven't written any code regarding that yet . Only function that asks for input a string.
read below
Last edited on
Try using string::find_first_of().
@TarikNeaj I didn't even meant that.. I DON'T want to compare string but i want to compare WORDS in string.
Say i wrote as input an string : "This word is the same word" . And i want to outpout the same string but changing the color of console when it encounters "word" because "word" is repeated two times .
Last edited on
Whoopsie daisy. Even the mighty must fall one day. Try what @dhayden told ya :)
I can't use string::find_first_of() for that.
Anyone helps?
why not just tokenize the string based on punctuation and ws?
Now i splitted string into words but i have last question . How to compare words ex if i have inputted string "Words repeated words" (i have function to split it into words) then how to make algorithm to recognize that someone repeated words ? Iterate through vector and ??

@Edit

I wrote something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

	HANDLE hOut;

	std::string text;

	std::cout << "Write text : ";

	getline(std::cin, text);

	std::vector<std::string> splited = split(text, ' ');

	std::vector<std::string> text2;

	if (splited.empty())
	{
		std::cout << "You didn't wrote any text ! " << std::endl;
	}

	for (int i = 0; i < splited.size(); i++)
	{
		if (i > 0)
		{
			text2.push_back(splited[i - 1]);
		}

		for (int j = 0; j < text2.size(); j++)
		{
			hOut = GetStdHandle(STD_OUTPUT_HANDLE);

			if (text2[j] == splited[i])
				SetConsoleTextAttribute(hOut, FOREGROUND_RED);

			std::cout << splited[i] << " ";
		}

	}


It works but not completely because when i input example : "something no something " then i got input "no something something " . How to fix that ?
Last edited on
are you asking how to record what words were written? i suggest using std::map. ie:
std::map<std::string, int>, so then you can do this: some_map[current_word]++
@Up Read my last post.
Any help ? -,-
Topic archived. No new replies allowed.