How can I improve a search using strtok() function?
I have a file "Shalespeare_Hamlet.txt" and it contains
Hamlet, some text, Hamlet, SAMPLE TEXT.
A programm have to search the number of occurrences of words in the text.
How can i improve my code using strtok()?
using namespace std;
int main()
const int len = 101;
char word[len], line[len];
cout << "Word: ";
cin >> word;
int l_word = strlen(word);
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Error! Can't open the file.";
system("pause");
return 1;
int count = 0;
while (fin.getline(line, len))
char *p = line;
while (p = strstr(p, word)) (*p == '')) count++;
cout << "Count of words: " << word << ": " << count << endl;
system("pause");
Thank you!
c++
|
show 6 more comments
I have a file "Shalespeare_Hamlet.txt" and it contains
Hamlet, some text, Hamlet, SAMPLE TEXT.
A programm have to search the number of occurrences of words in the text.
How can i improve my code using strtok()?
using namespace std;
int main()
const int len = 101;
char word[len], line[len];
cout << "Word: ";
cin >> word;
int l_word = strlen(word);
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Error! Can't open the file.";
system("pause");
return 1;
int count = 0;
while (fin.getline(line, len))
char *p = line;
while (p = strstr(p, word)) (*p == '')) count++;
cout << "Count of words: " << word << ": " << count << endl;
system("pause");
Thank you!
c++
1
Sounds like a XY problem. Rather than focusing onstrtok
(your current solution), why not tell us what problem you actually want to solve (so we may possibly suggest better solutions)?
– Jesper Juhl
Nov 11 at 14:05
3
strtok()
should be the last resort. You're using C++, not low-level C.
– PaulMcKenzie
Nov 11 at 14:06
"How can I improve a search using strtok() function?" You can improve your code by usingstd::string::find()
.
– Swordfish
Nov 11 at 14:07
1
@AlexDisander Then you have either a delusional task or a misunderstanding.
– Passer By
Nov 11 at 14:14
1
@AlexDisander So Swordwish is right -- you are learningC
with some C++ syntax thrown in.
– PaulMcKenzie
Nov 11 at 14:18
|
show 6 more comments
I have a file "Shalespeare_Hamlet.txt" and it contains
Hamlet, some text, Hamlet, SAMPLE TEXT.
A programm have to search the number of occurrences of words in the text.
How can i improve my code using strtok()?
using namespace std;
int main()
const int len = 101;
char word[len], line[len];
cout << "Word: ";
cin >> word;
int l_word = strlen(word);
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Error! Can't open the file.";
system("pause");
return 1;
int count = 0;
while (fin.getline(line, len))
char *p = line;
while (p = strstr(p, word)) (*p == '')) count++;
cout << "Count of words: " << word << ": " << count << endl;
system("pause");
Thank you!
c++
I have a file "Shalespeare_Hamlet.txt" and it contains
Hamlet, some text, Hamlet, SAMPLE TEXT.
A programm have to search the number of occurrences of words in the text.
How can i improve my code using strtok()?
using namespace std;
int main()
const int len = 101;
char word[len], line[len];
cout << "Word: ";
cin >> word;
int l_word = strlen(word);
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Error! Can't open the file.";
system("pause");
return 1;
int count = 0;
while (fin.getline(line, len))
char *p = line;
while (p = strstr(p, word)) (*p == '')) count++;
cout << "Count of words: " << word << ": " << count << endl;
system("pause");
Thank you!
c++
c++
asked Nov 11 at 14:01
Alex Disander
132
132
1
Sounds like a XY problem. Rather than focusing onstrtok
(your current solution), why not tell us what problem you actually want to solve (so we may possibly suggest better solutions)?
– Jesper Juhl
Nov 11 at 14:05
3
strtok()
should be the last resort. You're using C++, not low-level C.
– PaulMcKenzie
Nov 11 at 14:06
"How can I improve a search using strtok() function?" You can improve your code by usingstd::string::find()
.
– Swordfish
Nov 11 at 14:07
1
@AlexDisander Then you have either a delusional task or a misunderstanding.
– Passer By
Nov 11 at 14:14
1
@AlexDisander So Swordwish is right -- you are learningC
with some C++ syntax thrown in.
– PaulMcKenzie
Nov 11 at 14:18
|
show 6 more comments
1
Sounds like a XY problem. Rather than focusing onstrtok
(your current solution), why not tell us what problem you actually want to solve (so we may possibly suggest better solutions)?
– Jesper Juhl
Nov 11 at 14:05
3
strtok()
should be the last resort. You're using C++, not low-level C.
– PaulMcKenzie
Nov 11 at 14:06
"How can I improve a search using strtok() function?" You can improve your code by usingstd::string::find()
.
– Swordfish
Nov 11 at 14:07
1
@AlexDisander Then you have either a delusional task or a misunderstanding.
– Passer By
Nov 11 at 14:14
1
@AlexDisander So Swordwish is right -- you are learningC
with some C++ syntax thrown in.
– PaulMcKenzie
Nov 11 at 14:18
1
1
Sounds like a XY problem. Rather than focusing on
strtok
(your current solution), why not tell us what problem you actually want to solve (so we may possibly suggest better solutions)?– Jesper Juhl
Nov 11 at 14:05
Sounds like a XY problem. Rather than focusing on
strtok
(your current solution), why not tell us what problem you actually want to solve (so we may possibly suggest better solutions)?– Jesper Juhl
Nov 11 at 14:05
3
3
strtok()
should be the last resort. You're using C++, not low-level C.– PaulMcKenzie
Nov 11 at 14:06
strtok()
should be the last resort. You're using C++, not low-level C.– PaulMcKenzie
Nov 11 at 14:06
"How can I improve a search using strtok() function?" You can improve your code by using
std::string::find()
.– Swordfish
Nov 11 at 14:07
"How can I improve a search using strtok() function?" You can improve your code by using
std::string::find()
.– Swordfish
Nov 11 at 14:07
1
1
@AlexDisander Then you have either a delusional task or a misunderstanding.
– Passer By
Nov 11 at 14:14
@AlexDisander Then you have either a delusional task or a misunderstanding.
– Passer By
Nov 11 at 14:14
1
1
@AlexDisander So Swordwish is right -- you are learning
C
with some C++ syntax thrown in.– PaulMcKenzie
Nov 11 at 14:18
@AlexDisander So Swordwish is right -- you are learning
C
with some C++ syntax thrown in.– PaulMcKenzie
Nov 11 at 14:18
|
show 6 more comments
3 Answers
3
active
oldest
votes
I completely redid the code hope that help
using namespace std;
int main()
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Opening error!!!n";
system("PAUSE");
return EXIT_FAILURE;
fin.seekg(0, ios::end);
long length = fin.tellg();
char *file = new char[length + 1];
fin.seekg(0, ios::beg);
fin.read(file, length);
int k = 0;
char* arr[50000];
char* str;
str = strtok(file, "n ,.-!?:;");
while (str != NULL)
arr[k] = str;
k++;
str = strtok(NULL, "n ,.-!?:;");
char word[20];
cout << "Enter word: ";
gets_s(word);
int count = 0;
for (int i = 0; i < k; i++)
if (strcmp(arr[i], word) == 0)
count++;
cout << "Count of words: " << word << " : "<< count << endl;
delete file;
fin.close();
system("PAUSE");
return EXIT_SUCCESS;
You forgot as punctuation the quotation mark(s), bracket, etc. etc. It would be easier to remove the punctuation marks first (replace each with spaces by callingispunct()
to test the character), and then go over the adjusted input a second time to extract the words. Then the call tostrtok
would just need' '
as the delimiter instead of a long stream of punctuation characters.
– PaulMcKenzie
Nov 11 at 14:55
Also, if you look at std::ispunct, you will see that using the defaultC
locale, the punctuation characters are!"#$%&'()*+,-./:;<=>?@[]^_``~
. So your solution is not complete, since the OP explicitly usesispunct
in their post to determine if a character is a punctuation.
– PaulMcKenzie
Nov 11 at 15:05
Also, there is a problem with part words, for example: "SHamlet" will be considered also, when you're looking for "Hamlet", in addition you're not cleaning white spaces such as't'
and'r'
.
– SHR
Nov 11 at 15:09
add a comment |
You can't "improve" the program with strtok
in C++ when there are better facilities like std::string
and iostreams to solve the job.
how does that answer the question?
– Fureeish
Nov 11 at 14:35
@Fureeish I regarded the implementation to be very C-style have and unsecure (doesn't handle lines or words over 100 characters long). Hinting at string and iostream usage to solve it better.
– Bo R
Nov 11 at 14:45
add a comment |
Your code won't be working properly in several cases:
- If the searched word is in position in line which is split between two gets, for example the word "Hamlet" started in char position 98 in the line and split to "Ham" and "let".
- If there is punctuation in the beginning of the word, like "Hamlet... you only check the end of the string but not the beginning.
I think your algorithm should be as follows:
- Read whole line into a
std::string
usingstd::getline
- Remove all punctuation from the line,
- Use std::istringstream to split line into words.
- For each word check if equal to input word, and if so increment count.
For example:
std::string line;
//read whole line to std::string
while (std::getline(fin, line))
std::string result;
//remove punctuation and copy to result
std::remove_copy_if(line.begin(), line.end(), std::back_inserter(result), std::ptr_fun<int, int>(&std::ispunct) );
//use string stream to parse line without punctuation
std::istringstream istr(result);
std::string str;
while(istr>>str)
if(word==str)
count++;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249498%2fhow-can-i-improve-a-search-using-strtok-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I completely redid the code hope that help
using namespace std;
int main()
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Opening error!!!n";
system("PAUSE");
return EXIT_FAILURE;
fin.seekg(0, ios::end);
long length = fin.tellg();
char *file = new char[length + 1];
fin.seekg(0, ios::beg);
fin.read(file, length);
int k = 0;
char* arr[50000];
char* str;
str = strtok(file, "n ,.-!?:;");
while (str != NULL)
arr[k] = str;
k++;
str = strtok(NULL, "n ,.-!?:;");
char word[20];
cout << "Enter word: ";
gets_s(word);
int count = 0;
for (int i = 0; i < k; i++)
if (strcmp(arr[i], word) == 0)
count++;
cout << "Count of words: " << word << " : "<< count << endl;
delete file;
fin.close();
system("PAUSE");
return EXIT_SUCCESS;
You forgot as punctuation the quotation mark(s), bracket, etc. etc. It would be easier to remove the punctuation marks first (replace each with spaces by callingispunct()
to test the character), and then go over the adjusted input a second time to extract the words. Then the call tostrtok
would just need' '
as the delimiter instead of a long stream of punctuation characters.
– PaulMcKenzie
Nov 11 at 14:55
Also, if you look at std::ispunct, you will see that using the defaultC
locale, the punctuation characters are!"#$%&'()*+,-./:;<=>?@[]^_``~
. So your solution is not complete, since the OP explicitly usesispunct
in their post to determine if a character is a punctuation.
– PaulMcKenzie
Nov 11 at 15:05
Also, there is a problem with part words, for example: "SHamlet" will be considered also, when you're looking for "Hamlet", in addition you're not cleaning white spaces such as't'
and'r'
.
– SHR
Nov 11 at 15:09
add a comment |
I completely redid the code hope that help
using namespace std;
int main()
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Opening error!!!n";
system("PAUSE");
return EXIT_FAILURE;
fin.seekg(0, ios::end);
long length = fin.tellg();
char *file = new char[length + 1];
fin.seekg(0, ios::beg);
fin.read(file, length);
int k = 0;
char* arr[50000];
char* str;
str = strtok(file, "n ,.-!?:;");
while (str != NULL)
arr[k] = str;
k++;
str = strtok(NULL, "n ,.-!?:;");
char word[20];
cout << "Enter word: ";
gets_s(word);
int count = 0;
for (int i = 0; i < k; i++)
if (strcmp(arr[i], word) == 0)
count++;
cout << "Count of words: " << word << " : "<< count << endl;
delete file;
fin.close();
system("PAUSE");
return EXIT_SUCCESS;
You forgot as punctuation the quotation mark(s), bracket, etc. etc. It would be easier to remove the punctuation marks first (replace each with spaces by callingispunct()
to test the character), and then go over the adjusted input a second time to extract the words. Then the call tostrtok
would just need' '
as the delimiter instead of a long stream of punctuation characters.
– PaulMcKenzie
Nov 11 at 14:55
Also, if you look at std::ispunct, you will see that using the defaultC
locale, the punctuation characters are!"#$%&'()*+,-./:;<=>?@[]^_``~
. So your solution is not complete, since the OP explicitly usesispunct
in their post to determine if a character is a punctuation.
– PaulMcKenzie
Nov 11 at 15:05
Also, there is a problem with part words, for example: "SHamlet" will be considered also, when you're looking for "Hamlet", in addition you're not cleaning white spaces such as't'
and'r'
.
– SHR
Nov 11 at 15:09
add a comment |
I completely redid the code hope that help
using namespace std;
int main()
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Opening error!!!n";
system("PAUSE");
return EXIT_FAILURE;
fin.seekg(0, ios::end);
long length = fin.tellg();
char *file = new char[length + 1];
fin.seekg(0, ios::beg);
fin.read(file, length);
int k = 0;
char* arr[50000];
char* str;
str = strtok(file, "n ,.-!?:;");
while (str != NULL)
arr[k] = str;
k++;
str = strtok(NULL, "n ,.-!?:;");
char word[20];
cout << "Enter word: ";
gets_s(word);
int count = 0;
for (int i = 0; i < k; i++)
if (strcmp(arr[i], word) == 0)
count++;
cout << "Count of words: " << word << " : "<< count << endl;
delete file;
fin.close();
system("PAUSE");
return EXIT_SUCCESS;
I completely redid the code hope that help
using namespace std;
int main()
ifstream fin("Shakespeare_Hamlet.txt");
if (!fin)
cout << "Opening error!!!n";
system("PAUSE");
return EXIT_FAILURE;
fin.seekg(0, ios::end);
long length = fin.tellg();
char *file = new char[length + 1];
fin.seekg(0, ios::beg);
fin.read(file, length);
int k = 0;
char* arr[50000];
char* str;
str = strtok(file, "n ,.-!?:;");
while (str != NULL)
arr[k] = str;
k++;
str = strtok(NULL, "n ,.-!?:;");
char word[20];
cout << "Enter word: ";
gets_s(word);
int count = 0;
for (int i = 0; i < k; i++)
if (strcmp(arr[i], word) == 0)
count++;
cout << "Count of words: " << word << " : "<< count << endl;
delete file;
fin.close();
system("PAUSE");
return EXIT_SUCCESS;
answered Nov 11 at 14:46
Gilvius
442
442
You forgot as punctuation the quotation mark(s), bracket, etc. etc. It would be easier to remove the punctuation marks first (replace each with spaces by callingispunct()
to test the character), and then go over the adjusted input a second time to extract the words. Then the call tostrtok
would just need' '
as the delimiter instead of a long stream of punctuation characters.
– PaulMcKenzie
Nov 11 at 14:55
Also, if you look at std::ispunct, you will see that using the defaultC
locale, the punctuation characters are!"#$%&'()*+,-./:;<=>?@[]^_``~
. So your solution is not complete, since the OP explicitly usesispunct
in their post to determine if a character is a punctuation.
– PaulMcKenzie
Nov 11 at 15:05
Also, there is a problem with part words, for example: "SHamlet" will be considered also, when you're looking for "Hamlet", in addition you're not cleaning white spaces such as't'
and'r'
.
– SHR
Nov 11 at 15:09
add a comment |
You forgot as punctuation the quotation mark(s), bracket, etc. etc. It would be easier to remove the punctuation marks first (replace each with spaces by callingispunct()
to test the character), and then go over the adjusted input a second time to extract the words. Then the call tostrtok
would just need' '
as the delimiter instead of a long stream of punctuation characters.
– PaulMcKenzie
Nov 11 at 14:55
Also, if you look at std::ispunct, you will see that using the defaultC
locale, the punctuation characters are!"#$%&'()*+,-./:;<=>?@[]^_``~
. So your solution is not complete, since the OP explicitly usesispunct
in their post to determine if a character is a punctuation.
– PaulMcKenzie
Nov 11 at 15:05
Also, there is a problem with part words, for example: "SHamlet" will be considered also, when you're looking for "Hamlet", in addition you're not cleaning white spaces such as't'
and'r'
.
– SHR
Nov 11 at 15:09
You forgot as punctuation the quotation mark(s), bracket, etc. etc. It would be easier to remove the punctuation marks first (replace each with spaces by calling
ispunct()
to test the character), and then go over the adjusted input a second time to extract the words. Then the call to strtok
would just need ' '
as the delimiter instead of a long stream of punctuation characters.– PaulMcKenzie
Nov 11 at 14:55
You forgot as punctuation the quotation mark(s), bracket, etc. etc. It would be easier to remove the punctuation marks first (replace each with spaces by calling
ispunct()
to test the character), and then go over the adjusted input a second time to extract the words. Then the call to strtok
would just need ' '
as the delimiter instead of a long stream of punctuation characters.– PaulMcKenzie
Nov 11 at 14:55
Also, if you look at std::ispunct, you will see that using the default
C
locale, the punctuation characters are !"#$%&'()*+,-./:;<=>?@[]^_``~
. So your solution is not complete, since the OP explicitly uses ispunct
in their post to determine if a character is a punctuation.– PaulMcKenzie
Nov 11 at 15:05
Also, if you look at std::ispunct, you will see that using the default
C
locale, the punctuation characters are !"#$%&'()*+,-./:;<=>?@[]^_``~
. So your solution is not complete, since the OP explicitly uses ispunct
in their post to determine if a character is a punctuation.– PaulMcKenzie
Nov 11 at 15:05
Also, there is a problem with part words, for example: "SHamlet" will be considered also, when you're looking for "Hamlet", in addition you're not cleaning white spaces such as
't'
and 'r'
.– SHR
Nov 11 at 15:09
Also, there is a problem with part words, for example: "SHamlet" will be considered also, when you're looking for "Hamlet", in addition you're not cleaning white spaces such as
't'
and 'r'
.– SHR
Nov 11 at 15:09
add a comment |
You can't "improve" the program with strtok
in C++ when there are better facilities like std::string
and iostreams to solve the job.
how does that answer the question?
– Fureeish
Nov 11 at 14:35
@Fureeish I regarded the implementation to be very C-style have and unsecure (doesn't handle lines or words over 100 characters long). Hinting at string and iostream usage to solve it better.
– Bo R
Nov 11 at 14:45
add a comment |
You can't "improve" the program with strtok
in C++ when there are better facilities like std::string
and iostreams to solve the job.
how does that answer the question?
– Fureeish
Nov 11 at 14:35
@Fureeish I regarded the implementation to be very C-style have and unsecure (doesn't handle lines or words over 100 characters long). Hinting at string and iostream usage to solve it better.
– Bo R
Nov 11 at 14:45
add a comment |
You can't "improve" the program with strtok
in C++ when there are better facilities like std::string
and iostreams to solve the job.
You can't "improve" the program with strtok
in C++ when there are better facilities like std::string
and iostreams to solve the job.
answered Nov 11 at 14:32
Bo R
616110
616110
how does that answer the question?
– Fureeish
Nov 11 at 14:35
@Fureeish I regarded the implementation to be very C-style have and unsecure (doesn't handle lines or words over 100 characters long). Hinting at string and iostream usage to solve it better.
– Bo R
Nov 11 at 14:45
add a comment |
how does that answer the question?
– Fureeish
Nov 11 at 14:35
@Fureeish I regarded the implementation to be very C-style have and unsecure (doesn't handle lines or words over 100 characters long). Hinting at string and iostream usage to solve it better.
– Bo R
Nov 11 at 14:45
how does that answer the question?
– Fureeish
Nov 11 at 14:35
how does that answer the question?
– Fureeish
Nov 11 at 14:35
@Fureeish I regarded the implementation to be very C-style have and unsecure (doesn't handle lines or words over 100 characters long). Hinting at string and iostream usage to solve it better.
– Bo R
Nov 11 at 14:45
@Fureeish I regarded the implementation to be very C-style have and unsecure (doesn't handle lines or words over 100 characters long). Hinting at string and iostream usage to solve it better.
– Bo R
Nov 11 at 14:45
add a comment |
Your code won't be working properly in several cases:
- If the searched word is in position in line which is split between two gets, for example the word "Hamlet" started in char position 98 in the line and split to "Ham" and "let".
- If there is punctuation in the beginning of the word, like "Hamlet... you only check the end of the string but not the beginning.
I think your algorithm should be as follows:
- Read whole line into a
std::string
usingstd::getline
- Remove all punctuation from the line,
- Use std::istringstream to split line into words.
- For each word check if equal to input word, and if so increment count.
For example:
std::string line;
//read whole line to std::string
while (std::getline(fin, line))
std::string result;
//remove punctuation and copy to result
std::remove_copy_if(line.begin(), line.end(), std::back_inserter(result), std::ptr_fun<int, int>(&std::ispunct) );
//use string stream to parse line without punctuation
std::istringstream istr(result);
std::string str;
while(istr>>str)
if(word==str)
count++;
add a comment |
Your code won't be working properly in several cases:
- If the searched word is in position in line which is split between two gets, for example the word "Hamlet" started in char position 98 in the line and split to "Ham" and "let".
- If there is punctuation in the beginning of the word, like "Hamlet... you only check the end of the string but not the beginning.
I think your algorithm should be as follows:
- Read whole line into a
std::string
usingstd::getline
- Remove all punctuation from the line,
- Use std::istringstream to split line into words.
- For each word check if equal to input word, and if so increment count.
For example:
std::string line;
//read whole line to std::string
while (std::getline(fin, line))
std::string result;
//remove punctuation and copy to result
std::remove_copy_if(line.begin(), line.end(), std::back_inserter(result), std::ptr_fun<int, int>(&std::ispunct) );
//use string stream to parse line without punctuation
std::istringstream istr(result);
std::string str;
while(istr>>str)
if(word==str)
count++;
add a comment |
Your code won't be working properly in several cases:
- If the searched word is in position in line which is split between two gets, for example the word "Hamlet" started in char position 98 in the line and split to "Ham" and "let".
- If there is punctuation in the beginning of the word, like "Hamlet... you only check the end of the string but not the beginning.
I think your algorithm should be as follows:
- Read whole line into a
std::string
usingstd::getline
- Remove all punctuation from the line,
- Use std::istringstream to split line into words.
- For each word check if equal to input word, and if so increment count.
For example:
std::string line;
//read whole line to std::string
while (std::getline(fin, line))
std::string result;
//remove punctuation and copy to result
std::remove_copy_if(line.begin(), line.end(), std::back_inserter(result), std::ptr_fun<int, int>(&std::ispunct) );
//use string stream to parse line without punctuation
std::istringstream istr(result);
std::string str;
while(istr>>str)
if(word==str)
count++;
Your code won't be working properly in several cases:
- If the searched word is in position in line which is split between two gets, for example the word "Hamlet" started in char position 98 in the line and split to "Ham" and "let".
- If there is punctuation in the beginning of the word, like "Hamlet... you only check the end of the string but not the beginning.
I think your algorithm should be as follows:
- Read whole line into a
std::string
usingstd::getline
- Remove all punctuation from the line,
- Use std::istringstream to split line into words.
- For each word check if equal to input word, and if so increment count.
For example:
std::string line;
//read whole line to std::string
while (std::getline(fin, line))
std::string result;
//remove punctuation and copy to result
std::remove_copy_if(line.begin(), line.end(), std::back_inserter(result), std::ptr_fun<int, int>(&std::ispunct) );
//use string stream to parse line without punctuation
std::istringstream istr(result);
std::string str;
while(istr>>str)
if(word==str)
count++;
answered Nov 11 at 14:51
SHR
5,73552341
5,73552341
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249498%2fhow-can-i-improve-a-search-using-strtok-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Sounds like a XY problem. Rather than focusing on
strtok
(your current solution), why not tell us what problem you actually want to solve (so we may possibly suggest better solutions)?– Jesper Juhl
Nov 11 at 14:05
3
strtok()
should be the last resort. You're using C++, not low-level C.– PaulMcKenzie
Nov 11 at 14:06
"How can I improve a search using strtok() function?" You can improve your code by using
std::string::find()
.– Swordfish
Nov 11 at 14:07
1
@AlexDisander Then you have either a delusional task or a misunderstanding.
– Passer By
Nov 11 at 14:14
1
@AlexDisander So Swordwish is right -- you are learning
C
with some C++ syntax thrown in.– PaulMcKenzie
Nov 11 at 14:18