#include <iostream>
#include <cstring>
using namespace std;
void main()
{
char string[100], keyword[100], *string_ptr = string, *keyword_ptr=keyword;
cout << "Enter sentence\n";
cin.getline(string_ptr,100,'\n');
cout << "Enter search word\n";
cin.getline(keyword_ptr,100,'\n');
string_ptr = strtok(string, " ");
while (string_ptr != NULL)
{
//cout << string_ptr <<endl;
string_ptr = strtok(NULL , " ");
}
keyword_ptr = strtok(keyword, " ");
while (keyword_ptr != NULL)
{
//cout << keyword_ptr << endl;
keyword_ptr = strtok(NULL , " ");
}
}
How to find word in a string
Page 1 of 13 Replies - 120 Views - Last Post: 26 January 2013 - 08:50 AM
#1
How to find word in a string
Posted 26 January 2013 - 07:45 AM
I m working how to make a program to find word in a string. for this i posted my code it has logical error can any tell me what to do next?? i m tokenizing string after that what to do? thanks in advance..
Replies To: How to find word in a string
#2
Re: How to find word in a string
Posted 26 January 2013 - 08:05 AM
Is there a reason you are using C-strings instead of std::string? Using std::string should make things much easier.
Jim
Jim
#3
Re: How to find word in a string
Posted 26 January 2013 - 08:13 AM
now if we use std::string then what is the logic can u help me??
#4
Re: How to find word in a string
Posted 26 January 2013 - 08:50 AM
Use a stringstream and the stream extraction operator>> to extract "words" from the string.
Jim
vector<string> words;
string line("This is a test string");
istringsteam ins(line);
string temp;
while(ins >> temp)
words.push_back(temp);
Jim
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|