473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Search for word

2
Hi. I am new to c++ coding and I need help with a program I am trying to create.

The programs purpose is that you let the user write a sentence and then the user can search the sentence for a word and if it finds one in the sentence the console will then tell the user its correct and so on.

But I have done the same program with string, but I wish to create it with char arrays this time but I fail miserably and I can't figure it out. the problem is that The user cant find the word in the sentence. Well the code is here:::

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3.  
  4. int main(int argc, char** argv)
  5. {
  6.     int get = 0;
  7.  
  8.     char sentence[56];
  9.     char word[10];
  10.  
  11.     std::cout << "Please write a sentance; \n";
  12.  
  13.  
  14.     std::cin.getline(sentence, 56);
  15.  
  16.     std::cout <<"your sentance is: \n" << sentence << std::endl;
  17.  
  18.     std::cout <<"Now search for a word in your sentance:"<< std::endl;
  19.  
  20.     std::cin >> word;
  21.  
  22.  
  23. while ( get < 56)
  24. {
  25.     if (sentence[get] == word[1])
  26.     {
  27.     get++;
  28.         if (sentence[get] == word[1])
  29.         {
  30.             std::cout << "Thats a word in your sentence\n\n";
  31.             break;
  32.         }
  33.         else
  34.         {
  35.             get++;
  36.         }
  37.  
  38.     }
  39.     else
  40.     {
  41.  
  42.         std::cout << "Sorry, thats not a word in your sentence.\n\n";
  43.         get++;
  44.         break;
  45.     }
  46.  
  47. }
  48.  
  49.  
  50.     return 0;
  51.  
  52. }
Thanks for any help!!

I am prolly just really confused...
Oct 19 '11 #1
2 2334
Banfa
9,065 Expert Mod 8TB
You need a nested loop to do this, the outer loop loops for the occurrence of the first letter in the word (possibly at the start of a word), the inner loop runs if the first letter is found and checks the other letters; then if it fails half way through you can continue the search at the next letter in the sentence on the outer loop rather.

If you try to do this with a single loop you end up skipping letters in the sentence when looking for the first letter of the word.

Also for some reason you only ever compare the 2nd letter of the word.
Oct 19 '11 #2
Mite22
2
Thanks for the reply!

Would it be possible for you to explain it a bit more. Because I don't understand how you can create loops when you don't know what the user write for sentence and how big the words are.

Thanks!!
Oct 20 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: David Morgan | last post by:
Hello I have a little function to highlight text if it exists. Function Highlight(vFind, vSearch) Dim RegEx Set RegEx = New RegExp RegEx.Pattern = vFind RegEx.IgnoreCase = True Highlight =...
1
by: chrisben | last post by:
Hi, Is there a quick easy way to search a text for words in richtextbox? I would like to make it work as a word document, as the having Find, Find Next by right click the richtextbox. THe...
5
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word...
1
by: omri1e | last post by:
Hi, i want to search (using html page) for a string in a txt file that contain lines that in each line there is a user name and date, if i search for the user name it will show the date also thaks...
0
by: widyasatria | last post by:
I like to search word humble in a word my humble home in visual basic 6 is there any suggestion ?
1
by: abhilash12 | last post by:
hai i can't search word in open office og doc files save as type is microsoft word 97/2000/xp (.doc) using java pls help me
1
by: abhilash12 | last post by:
hai how can i search word using java from open office og doc file pls help me
1
by: abhilash12 | last post by:
hai i have to use search word from files in web application so pls telll me is there any search engine for using search word in doc files Thanks And Regards abhilash
2
by: raamay | last post by:
i have seen various tutorials on how to search a database but havent found one which teaches how to search a word or a string anywhere in a database. Well, a user may type 'hel' in the search box and...
3
by: ramsham | last post by:
Hi I want to search a particular word from text and if found match in between then highlight that word where ever it appears.Then if user clicks on that word it shows some information related to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.