Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ find the word in a particular sentence

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;
    
}
 
Have you stepped through this code it in a debugger, verifying the program flow and examining all these values as you go ?

In this bit of code:

Code:
if (ptr_sentence[j] == word[j])
    rate_of_smilarity++;
    temp_distance = distance;

aren't you missing a pair of brackets ? Either that, or your indentation sucks. Note that I do not grasp your program logic with all the different variables.

Sorry to be a bit critical, but when I see sloppy bracketing and indentation, spelling errors like lenght, world, smilarity, and long incomprehensible sentences like "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" I do get worried about the accuracy of the programmer.... Programming is a very exact science. If I can give one tip to beginning programmers, it is that being obsessively clear, concise and consistent really pays off. Especially if you ask for someone's help 😉

I have a feeling this program might crash at the end of the loop because you have one iteration too many:

for (int i = 0; i <= lenght; i++)

but this being C++ I am not 100% sure. I don't like that you first create a C++ string object, then immediately convert it to an old-fashioned C-style char pointer, and proceed as if you are writing plain old C (which you might just as well have done).
 

Buy us a coffee!

Back
Top Bottom