RhoAies
New Coder
The purpose of the code I wrote is to find a word in a particular sentence and print its location. To summarize, I tried to do this, I got 2 entries, the first is a sentence and the other is a word I put them in the array, then I start navigating through the sentence, if there is the first letter of the word , I enter the loop and if the word is the same as the word the similarity_rate is going to be as word long. There is no error in the code I wrote, but I can't see the output in the terminal.
C++:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sentence;
cout << "Enter the string : ";
getline(cin,sentence);
size_t lenght = sentence.size();
const char* ptr_sentence = sentence.c_str();
string key_word;
cout << "Which world do you want to find : ";
getline(cin, key_word);
size_t lenght_world = key_word.size();
const char* word = key_word.c_str();
int rate_of_smilarity = -1;
int distance = -1;
int temp_distance;
for (int i = 0; i <= lenght; i++) {
distance++;
if (ptr_sentence[i] == word[0])
for (int j = 0; j <= lenght_world; j++) {
if (ptr_sentence[j] == word[j])
rate_of_smilarity++;
temp_distance = distance;
}
else if (rate_of_smilarity == lenght_world)
cout << temp_distance;
}
return 0;
}