to find the number of times of a word in a sentence!

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 874 Views - Last Post: 08 August 2011 - 07:01 AM Rate Topic: -----

#1 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

to find the number of times of a word in a sentence!

Posted 07 August 2011 - 03:53 AM

hey guys!
i have written this code. All it has to do is just find the number of times a word comes in a sentence . but every time i run the thread is stopped and exe file is hanged pointing towards a linking or some other error but it compiles successfully . Can you point out the problem in my code plz .
#include <iostream.h>
#include <conio.h>
void main()
{
 char str1[10];
 char str2[30];
 cout<<"\n enter your sentence :"<<endl;
 cin.getline(str2,30,'\n');
 cout<<"\n enter your word :"<<endl;
 cin>>str1;
 int count =0,k=0,i=0,j=0,flag=0,word=0,n_word=0,h=0;
 while(str2[i]!='\0')

 {
  j=k;
   while(str2[j]!=' ')
     {
       if(str1[h]==str2[j])
         {
           count++;
            if(count==3)
               {word++; }
         }
     else
       { n_word++;}
      j++; k++;h++;
     }

   k=k+1;
 }
if(word!=0)
 { cout<<" \n word is found : "<<word<<" times";
  cout<<endl;}
else
  cout<<" word is not found : "<< n_word<<" times ";


 getch();
}



View Postezooo, on 07 August 2011 - 03:49 AM, said:

hey guys!
i have written this code. All it has to do is just find the number of times a word comes in a sentence . but every time i run the thread is stopped and exe file is hanged pointing towards a linking or some other error but it compiles successfully . Can you point out the problem in my code plz .
#include <iostream.h>
#include <conio.h>
void main()
{
 char str1[10];
 char str2[30];
 cout<<"\n enter your sentence :"<<endl;
 cin.getline(str2,30,'\n');
 cout<<"\n enter your word :"<<endl;
 cin>>str1;
 int count =0,k=0,i=0,j=0,flag=0,word=0,n_word=0,h=0;
 while(str2[i]!='\0')

 {
  j=k;
   while(str2[j]!=' ')
     {
       if(str1[h]==str2[j])
         {
           count++;
            if(count==3)
               {word++; }
         }
     else
       { n_word++;}
      j++; k++;h++;
     }

   k=k+1;
 }
if(word!=0)
 { cout<<" \n word is found : "<<word<<" times";
  cout<<endl;}
else
  cout<<" word is not found : "<< n_word<<" times ";


 getch();
}


if(str1[h]==str2[j])
         {
           count++;
            if(count==3)
               {word++; }
         }


here i have taken count==3 because to test my code i am initially taking a word of 3 characters. so plz just point out the major problem in my logic or code instead of little syntax mistakes.

Is This A Good Question/Topic? 0
  • +

Replies To: to find the number of times of a word in a sentence!

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 06:09 AM

You are accessing your arrays out of bounds in your while loops (h, j, k).

Also main should be defined as returning an int int main() and you should find an indentation style you like and use it consistently. Lastly you should consider changing your compiler to a more modern compiler. Any compiler that accepts #include <iostream.h> is obsolete and should be abandoned.


Jim

This post has been edited by jimblumberg: 07 August 2011 - 06:13 AM

Was This Post Helpful? 3
  • +
  • -

#3 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 10:19 AM

View Postjimblumberg, on 07 August 2011 - 06:09 AM, said:

You are accessing your arrays out of bounds in your while loops (h, j, k).

Also main should be defined as returning an int int main() and you should find an indentation style you like and use it consistently. Lastly you should consider changing your compiler to a more modern compiler. Any compiler that accepts #include <iostream.h> is obsolete and should be abandoned.


Jim

hmmm! well this is not the right piece ov solution . You are not making any sense. I am using Borland because we have to use this in this course and it is not the issue of compilor.
Was This Post Helpful? -4
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 10:38 AM

Quote

You are accessing your arrays out of bounds in your while loops (h, j, k).

Your array sizes are char str1[10] and char str2[30] your program is trying to access these arrays past the end of the arrays. Arrays start at 0 and end at size - 1, 9 and 29 it this case.

There may be other problems but fixing this will possibly fix your program. You can determine where these errors are detected by running your program through your debugger. The debugger will tell you where it first detects the problem.

The compiler issues should be discussed with your instructor. Using a compiler that is over 20 years old and is not standard compliant will hinder your efforts to become a qualified programmer.

The indentation issue will make your code easier to read and troubleshoot.


Jim

This post has been edited by jimblumberg: 07 August 2011 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 10:57 AM

ezoo0 said

Quote

hmmm! well this is not the right piece ov solution . You are not making any sense. I am using Borland because we have to use this in this course and it is not the issue of compilor.


Of course Jim Blumberg is making sense !!

Your compiler is accepting #include <iostream.h> !!

as a header man !! and is not saying anything about it....

so Jim is quite within reason to point this out to you

If you you have to use a borland compiler as part of your course work

then thats fine ... but its well out of date and you should be told that.

back to your program...

your program logic is really, really, really, bad I cant over emphasize

how bad it truly is...

you should have thought about this problem a bit more before you started

to program it at the least you should have pseudocoded it..

first of all i think you should ditch the magic numbers and

set up a few defines

for the length of a word

and

for the length of a sentence

secondly why limit yourself to 3 letter words when its just as easy to program this for any word length..

code to get you started

//Headers included

#define MAX_SENTENCE 100
#define MAX_WORD 15

int main()
{
    char str1[MAX_WORD];
    char str2[MAX_SENTENCE];

    std::cout<<"enter your sentence :"<<std::endl;
    std::cin.getline(str2,MAX_SENTENCE);
    int senLength = strlen(str2);

    std::cout<<"enter your word :"<<std::endl;
    std::cin.getline(str1,MAX_WORD);
    int wordLength = strlen(str1);



now you have two strings and two int's wordLength for the length of a word

and senLength for the length of a sentence

pseudocode for the rest of program

loop until the length of sentence length

compare the first character of str1 to str2
if the same increment the letter counting variable

if the letter counting variable is equal to the word length
and str2's next character is a space or a null terminator
then increment word counting variable and set letter counting variable to zero

if letter counting variable is equal to the word length
then set letter counting variable to zero

if str2 is a space or a null terminator
increment not a word counting variable
set letter counting variable to zero and
set variable used to loop through str1 to zero

if variable used to loop throught str1 is more than equal to word length
set variable used to loop through str1 to zero.

increment variable used to loop through str2

end loop


Best Wishes Snoopy.
Was This Post Helpful? 2
  • +
  • -

#6 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 11:15 AM

hey! can you explain this .if my compilor accepts #include<iostream.h> and why obsolete due to this. i mean what it should tell . aND I THOought a lot about this program and still thinking and writing but to in vain. but sure i will start again thinking and writing a code as u suggest . but following is the code i tried before your reply. and it also doesn't work . suggest me a compilor also
#include <iostream.h>
#include <conio.h>
#include <cstring.h>
void main()
{
 char str1[10];
 char str2[30];


 cout<<"\n enter your sentence :"<<endl;
 cin.getline(str2,30,'\n');
 cout<<"\n enter your word :"<<endl;
 cin>>str1;
 int l=strlen(str1);
 int ll=strlen(str2);
 cout<<"\n length of str 1: "<<l;
  cout<<"\n length of str 2: "<<ll;
 int count =0,k=0,i=0,j=0,flag=0,word=0,n_word=0,h=0;
 while(str2[i]!='\0')

 {
   i++;
 flag++;
  }
  cout<<"\n length of str2 :" <<flag;
   j=k;
   while(str2[j]!='\0')
     {
       if(str1[h]==str2[j])
         {
           count++;
            if(count==l)
               word++;
             else if(str2[j]==32)
              {
                  h=-1; j=j+1; count=-1;
              }

        }
     else

       { n_word++;}
      j++; k++;h++;


   k=k+1;
  }
if(word)
 { cout<<" \n word is found : "<<word<<" times";
  cout<<endl;}
else
  cout<<" word is not found : "<< n_word<<" times ";


 getch();
}

This post has been edited by ishkabible: 07 August 2011 - 01:09 PM
Reason for edit:: please don't quote the post above you, especially if it's long. thanks

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 11:23 AM

First why does it not work? What are your error messages?

Quote

if my compilor accepts #include<iostream.h> and why obsolete due to this.


The iostream.h header file is a pre-standard header file. The C++ standard headers do not have an extension and are very different than the pre-standard headers. The Borland compiler you are using was produced before C++ was standardized and is therefore not standard compliant.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 11:31 AM

View Postjimblumberg, on 07 August 2011 - 11:23 AM, said:

First why does it not work? What are your error messages?

Quote

if my compilor accepts #include<iostream.h> and why obsolete due to this.


The iostream.h header file is a pre-standard header file. The C++ standard headers do not have an extension and are very different than the pre-standard headers. The Borland compiler you are using was produced before C++ was standardized and is therefore not standard compliant.

Jim

it doesn't work. no error msgs not even a single one. only the output is making problem because it only compare first
single word in the sentence and if it matches it gives output 1 , if first word doesn't match then it is unable to compare and recognise next word . no matter it is same or not and prints the message that word is not found . and what does it mean? pre-standard headers and compilor?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 11:39 AM

When I ran your program with the following input:

Quote

enter your sentence :
This is a test.

enter your word :
is

I got this output:

Quote

length of str 1: 2
length of str 2: 15
length of str2 :15 word is not found : 15 times


So the program seems to be working, sometimes, but not producing the correct results. What were your inputs?

Quote

and what does it mean? pre-standard headers and compilor?


The C++ standard was ratified in about 1998. Your compiler was produced in about 1988.

Jim

This post has been edited by jimblumberg: 07 August 2011 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#10 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 11:41 AM

View Postjimblumberg, on 07 August 2011 - 11:39 AM, said:

When I ran your program with the following input:

Quote

enter your sentence :
This is a test.

enter your word :
is

I got this output:

Quote

length of str 1: 2
length of str 2: 15
length of str2 :15 word is not found : 15 times


So the program seems to be working, sometimes, but not producing the correct results. What were your inputs?

Jim

same outputs. sometimes it give right answer but most ov the time wrong answer . any suggestion ?
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 11:51 AM

I would suggest that you change your array sizes to much larger numbers. Also you probably should use getline() with both arrays. Why is it reporting "word is not found"? In my example my word should have been found. As stated by snoopy11 you need to rethink your logic. Are you allowed to use the functions in string.h, strtok(), strstr()?

Jim
Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 11:59 AM

This is agonizing. I would suggest you look into strtok() and strcmp().
Was This Post Helpful? 3
  • +
  • -

#13 ezooo  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 33
  • Joined: 26-June 11

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 12:08 PM

View PostButchDean, on 07 August 2011 - 11:59 AM, said:

This is agonizing. I would suggest you look into strtok() and strcmp().

not allowed to use built in functios :( okay rethinking nd then will post the correct code .
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • Joined: 20-March 10

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 12:58 PM

View PostButchDean, on 07 August 2011 - 11:59 AM, said:

This is agonizing. I would suggest you look into strtok() and strcmp().


That is so funny and also so true it is agonising...
I knew he wouldnt be able to use strtok that would have just been too easy

The sad thing is its so easy to make a working solution to this program without it..

Anyway Im giving you a rep point for making me laugh.

Best Wishes Snoopy.
Was This Post Helpful? 0
  • +
  • -

#15 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: to find the number of times of a word in a sentence!

Posted 07 August 2011 - 01:16 PM

i have been thinking that same thing for this whole post. any way the best way to do this would be to re-make them in some form or fashion. the really agonizing part about this is that you can't use std::vector or std::string...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2