Compare strings word by word C++ [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have two vectors containing strings. I want to compare each string of vector1 with each string of vector2 and check how many words are the same in both strings. The code I have only works if the two strings are perfectly similar :
Compare::Compare(vector<string> text1, vector<string> text2, int ratio)
{
text1Size_ = text1.size();
text2Size_ = text2.size();
if(text1Size_ > text2Size_)
{
totalWords_ = text1Size_;
}
else
{
totalWords_ = text2Size_;
}
it = text1.begin();
for(int i = 0; i < text1Size_; i++)
{
it2 = text2.begin();
for(int i = 0; i < text2Size_; i++)
{
if(*it == *it2)
{
cout << "Perfect match";
}
it2++;
}
it++;
}
}
I need to return each similar string if they have at least the ratio of similar words.
Is there a easier way than to parse each string, put each word in an array and compare them?
-EDIT-
By word I mean a written word like "bird". I'll give an example.
Let says I only have one string per vector and I need a 70% ratio of similarities:
string1 : The blue bird.
string2 : The bird.
What I want to do is to check if there is at least 60% of the written words that match in both sentences.
Here I have "The" and "Bird" that match. So I have 2/3 similar words (66.666%). So theses strings will be accepted.
-EDIT 2-
I don't think I can use ".compare()" here since it will check each character and not each written word...

Use a string stream to split a string into words:
#include <sstream>
bool is_similar(string str1, string str2)
{
vector<string> words1, words2;
string temp;
// Convert the first string to a list of words
std::stringstream stringstream1(str1);
while (stringstream1 >> temp)
words1.push_back(temp);
// Convert the second string to a list of words
std::stringstream stringstream2(str2);
while (stringstream2 >> temp)
words2.push_back(temp);
int num_of_identical_words = 0;
// Now, use the code you already have to count identical words
...
double ratio = (double)num_of_identical_words / words2.size();
return ratio > 0.6;
}

Related

How to count number of sentences in a string?

I am writing a program that counts the number of sentences in a string.
I count the number of '.' '?' '!'. However, there are Mr. Mrs. PhD. Dr. ..... situations. Any help please?
int number_of_sentences = 0;
for(unsigned int i=0; i <= text.length()-1; i++){
if(text[i] == '.' || text[i] == '?' ||text[i] == '!'){
++number_of_sentences;
}
}
return number_of_sentences;
You can't do it. You would need a full natural language parser to handle it with any accuracy.
Discarding the words you mention won't solve the problem. Consider:
I am impressed by that PhD. James was awarded.
I am impressed by that PhD. James was awarded it in 2001.
It is only your understanding of the semantics of English that tells you that the first one is one sentence and the second one is two sentences. You wouldn't be able to tell the difference without thinking about the meaning of the words, though. You are trying to solve the problem at the purely syntactic level, but there isn't enough information in the text without considering semantics.
The best approximation would probably be to say that you get a new sentence whenever you get a ".", "!" or "?" and the next word starts with a capital letter. But this would still be only approximately correct. It would get the first of these examples wrong, and the second one right.
Hint. Why don't you split the string in token? Then countdown every time there is a word as Mrs., Mr. ect..
Or replacing special words with white space then counting without problem.
std::string RemoveWords(const std::string& source, const std::string& chars) {
std::string result="";
for (unsigned int i=0; i<source.length(); i++) {
bool foundany=false;
for (unsigned int j=0; j<chars.length() && !foundany; j++) {
foundany=(source[i]==chars[j]);
}
if (!foundany) {
result+=source[i];
}
}
return result;
}
int number_of_sentences = 0;
text = RemoveWords(text);
for(unsigned int i=0; i <= text.length()-1; i++){
if(text[i] == '.' || text[i] == '?' ||text[i] == '!'){
++number_of_sentences;
}
}
return number_of_sentences;
The above solution will omit every character passed in the second argument string. For example:
std::string result=RemoveWords("Mrs. Rease will play to football. ByeBye", "Mrs.");

Need to set a array string length so that it is exactly 13 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
hi there i'm currently finishing a project at college but need to set a length of 13 to a string so that it will return an error message if it is shorter or longer than 13 my code so far is
void add_new_text_book()
{
//this option is here to add a new book to the list
printf("Please enter the Title of a new text book\n");
scanf("%s",book[number_of_books].title);
printf("Please enter the Authors firstname\n");
scanf("%s",book[number_of_books].firstname);
printf("Please enter the Author surname\n");
scanf("%s",book[number_of_books].surname);
printf("Finally please enter the ISBN number of the book\n");
scanf("%s",book[number_of_books].isbn);
if(length_of_string==13)//will be used to check the length of the book is valid
{
if(number_of_books==15)//will check to see how many records have been used
{
printf("book not added as you have used all free space\n");
}else
{
printf("Book has been added to the libary\n");
number_of_books=number_of_books+1;
}
}else{
printf("You have entered too many or few characters the books has not been saved\n");
}
getch();
length_of_string=strlen(book[number_of_books].isbn);
but even when i enter 13 it comes up with the error message it only seems to accept 123-456-789-1 any help will be greatly accepted
It would help considerably if you calculated length_of_string before you used it, rather than after.
You can use str.size() to do your calculations.
Taken from C++ reference
// string::size
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
std::cout << "The size of str is " << str.size() << " characters.\n";
return 0;
}
what the value of "length_of_string" ?
is its value is :123-456-789-1?
if so you can use it :
if(length_of_string.size()==13)//will be used to check the length of the book is valid
{
if(number_of_books.size()==15)//will check to see how many records have been used
{
printf("book not added as you have used all free space\n");
}else
{
printf("Book has been added to the libary\n");
number_of_books=number_of_books+1;
}
}
else
{
printf("You have entered too many or few characters the books has not been saved\n");
}
getch();
}

Reading string from vector until whitespace in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
This is probably easy but im not sure how. I tried searching multiple websites and yes Google and couldn't find anything on this.
My vector result[0] looks like this
A3 * * B4 * *
Declaration
vector<string> result = v.formVectorFile("Prj3 Config.txt");
I know that cin reads until whitespace so I was trying to use this to figure it out.
If I read straight from fstream I can read until whitespace, but im trying to do this with a string inside a vector and something like result[0] >> s; obviously doesnt work.
I need to read until it hits a whitespace then read the next one until whitespace. Etc...
So extract A3 by itself. Operate on it then extract * etc...
Your question is unclear because you don't tell us precisely what result is.
If we can assume that result is a std::vector<std::string>, then you can do something like this:
std::istringstream iss(result[0]); // consider only first string in vector
std::string item;
while(iss >> item) {
std::cout << "I found: " << item << "\n";
}
If we assume that result is std::vector<char>, then you can do this:
std::string s(result.begin(), result.end()); // consider entire vector as single string
std::istringstream iss(s);
while(iss >> item) {
std::cout << "I found: " << item << "\n";
}

characters and files in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have to make a program where the filename is composed of two parts: the first one is fixed and the second one can change during the program e.g "fixpart_integer.dat". I tried to do this in C++ but I did not succeed. The fisrt probelm is: how can i convert a number to a char ; and how can i concanate these 2 characters ; and how to declare this final char in the right way in order to open this filename ?
Many questions but I did find an easy way to do this.
#include <string>
#include <sstream>
std::string make_filename(std::string prefix, int id) {
std::stringstream ss;
ss << prefix << "_" << id << ".dat";
return ss.str();
}
and then I convert the string to char.
if you are working with c you may try the function described in here itoa
else if you are working woth c++ you may try something like this:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

Parsing list of strings ending with double NULL [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How to parse in c++ list of null-terminated Unicode strings where the list is terminated with two NULL characters?
There's this little example on Raymond Chen's blog (which, perhaps not surprisingly, is the first find in Google for "double null terminated string"):
This reinterpretation of a double-null-terminated string as really a
list of strings with an empty string as the terminator makes writing
code to walk through a double-null-terminated string quite
straightforward:
> for (LPTSTR pszz = pszzStart; *pszz; pszz += lstrlen(pszz) + 1) { ...
> do something with pszz ... }
The LPTSTR and lstrlen are wrappers which change depending on whether or not _UNICODE is set.
You simply build a list of strings and abort when one is empty:
std::vector<std::string> result;
result.push_back( std::string() );
while (std::cin) {
char c = std::cin.get();
if ( c == 0 ) {
if ( result.back().empty() ) { result.pop_back(); return; }
else result.push_back(std::string()); }
} else {
result.back().push_back(c);
}
}