Find a Word in a String Without Find Function

Split from Necro'd topic

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 14419 Views - Last Post: 03 December 2010 - 01:39 PM Rate Topic: -----

#1 Guest_silver90*


Reputation:

Find a Word in a String Without Find Function

Posted 27 November 2010 - 06:50 AM

i ran the code harshakirans provided but its giving me errors on my compiler.

i even made a code of my own, but i seem to be having trouble. it is a program to find a word in a string and print its no of occurences without using the find or any string library function.
could you please point me out where i am going to wrong, and help me sort it out.
i will really appreciate it.
:)


  #include <stdio.h>
#include <conio.h>
void main()
{
     char s[50],ch;
     int c=0;
	 char s2[20],th;
     
     printf("Enter any string : ");
     for(int i=0;ch!='\n';i++)
	 {
     ch=getchar();
     s[i]=ch;
     }
	  
     s[i]='\0';
	  
	
	 printf("enter word: ");
     for(int k=0;th!='\n';k++)
	 {
     th=getchar();
     s2[k]=th;
     }
	 s2[k]='\0';

    for(i=0;s[i]!='\0';i++){
   
	         if (s[i]==s2[i])
				while (s[i] !=' ')
				{	c++;

       while(s[i]==' ')
        i++;
    }
     }
     printf("\n\noccurences are %d",c+1);
     getch();
}  


Is This A Good Question/Topic? 0

Replies To: Find a Word in a String Without Find Function

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Find a Word in a String Without Find Function

Posted 27 November 2010 - 06:59 AM

If you're getting errors, you need to tell use EXACTLY what the errors are. Copy and paste them into the post.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 27 November 2010 - 06:11 PM

i had to add something to the code harshakirans wrote because i couldn't even use strlen() function. i am not supposed to include string library at all. so i made my own strlen function and added it to the program and got errors.
please do check my strlen function as well, but i think it's right. since it gave me correct outputs when i used it for concatenate and compare.
and please, could you also have a look at MY code i wrote up there. it doesn't give any errors but there is no output. maybe some for condition error. i know i am only a guest here, but help me out please.

anyway, here are the errors i got with harshakiran's code. do tell me if it's my compiler. i use microsoft visual c++ 6.0
*******************************************************************************************************************
Compiling...
l.cpp
C:\Users\ACS\Desktop\project q\garbage\l.cpp(32) : error C2664: 'strlen' : cannot convert parameter 1 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

l.obj - 1 error(s), 0 warning(s)
*******************************************************************************************************************

the strlen function i used was
strlen(char *s)
{
	int length = 0;
    while(*s != '\0')
	{
     length++;
     s++;
	}
    return(length);
}
  

Was This Post Helpful? 0

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1622
  • View blog
  • Posts: 3,080
  • Joined: 30-May 10

Re: Find a Word in a String Without Find Function

Posted 27 November 2010 - 10:50 PM

But there is no call to strlen() in your first post.

The error message is complaining about how you tried to call the function, not how it was defined.
Was This Post Helpful? 0
  • +
  • -

#5 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 27 November 2010 - 11:46 PM

i am sorry if i am confusing you guys.
i wrote the function on my own which i posted in the beginning of this post.(it doesn't need any strlen)
but it gave me no output. i was hoping to have anyone correct me as to where i was going wrong.

but then i also tried using the code harshakirans had posted and edited it slightly by adding my own strlen function in it. but even that didn't work. the errors i posted were from these.

i will REALLY appreciate it if you could correct me in my own code.
the one i posted in my first post.
Was This Post Helpful? 0

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Find a Word in a String Without Find Function

Posted 28 November 2010 - 02:31 AM

Repost your code as it is now.

Your 'first post' doesn't mean much now that your post has been split from the old one you started posting in.
When you post your code answer these questions.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it?

BTW - We are not going to 'correct your code' but we are happy to help you correct your code
Was This Post Helpful? 0
  • +
  • -

#7 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 28 November 2010 - 03:30 AM

my code is:
 #include <stdio.h>
#include <conio.h>
void main()
{
     char s[50],ch;
     int c=0;
	 char s2[20],th;
     
     printf("Enter any string : ");
     for(int i=0;ch!='\n';i++)
	 {
     ch=getchar();
     s[i]=ch;
     }
	  
     s[i]='\0';
	  
	
	 printf("enter word: ");
     for(int k=0;th!='\n';k++)
	 {
     th=getchar();
     s2[k]=th;
     }
	 s2[k]='\0';

    for(i=0;s[i]!='\0';i++){
   
	         if (s[i]==s2[i])
				while (s[i] !=' ')
				{	c++;

       while(s[i]==' ')
        i++;
    }
     }
     printf("\n\noccurences are %d",c+1);
     getch();
}


1-yes my code compiles. 0 errors 0 warnings.
2- the program sometimes produces outputs which is sometimes wrong and sometimes right, and sometimes it just doesn't produce any output at all.
for example:
string 1: is nobody here with me.
string 2(word of whose occurence is to be counted): is
output: none. no key works.
expected output:1
THE PROGRAM DOES NOT WORK

example 2:
string 1: nobody is here with me.
string 2: is
output:1
expected output:1
THE PROGRAM WORKS

example 3:
string 1: there is nobody who is here with me.
string 2: is
output:1
expected output:2
THE PROGRAM GIVES WRONG RESULT

i have been trying to fix this for days now. and i just have not been able to get it right, though i have a feeling its just an inch beyond my reach.
and, yes, i totally understand. i will love and will truly be grateful for whatever help i can get from you.
thankyou for being so patient with me.
Was This Post Helpful? 0

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Find a Word in a String Without Find Function

Posted 28 November 2010 - 03:55 AM

Okay debugging 101.
Add this debug output code to your code in the indicated location.
Compile, run and test your program.
What did you learn about your program?
	for(i=0;s[i]!='\0';i++){
		printf("s[%d] = %c || s2[%d] = %c \n",i,s[i],i,s2[i]);
		if (s[i]==s2[i])



BTW
"void main()" is wrong. Always wrong.
Always use "int main()".
Read here the words of the man who invented C++:
http://www.research.....html#void-main
Can I write "void main()"?
The definition void main() { /* ... */ } is not and never has been C++, nor has it ever been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1.

And read here about some of the problems void main() can cause
http://www.gidnetwork.com/b-66.html
Was This Post Helpful? 0
  • +
  • -

#9 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 28 November 2010 - 04:13 AM

oh i see it.
i need to change the index for the second string separately. i do that using another for loop and putting in more modifications.

how about this. this runs on my compiler error free and gives me the correct output as well. i shall go try with more test examples :D let me know if you spot a mistake, and then i shall point it out.

 for(i=0;s[i]!='\0';i++){

            for (int j=0; s[j]!='\0' ; j++)
			{
				if (s2[j]==s[i] )
				{  
			       i++;
				if (s[i+1]==' ' || s[i]=='.' )
				{	c++;
				}
				else
					break;
				
				}

				else
				{
				break;
				}
			}
       
	 }


BTW, THANKYOU.
i just realized how these discussion threads are SO helpful in making you think =)
Was This Post Helpful? 0

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Find a Word in a String Without Find Function

Posted 28 November 2010 - 04:37 AM

Well at least you are going to pass the first if() test sometimes now, so that's an improvement.

I am using the string "the dog" and the word "dog" as my test inputs.
If you look at your program behaviour in much the same way as before then we see we are still never reaching 'c++' using those test inputs so you need to think about this a lot more.

	for(i=0;s[i]!='\0';i++){
		int j;
		for (j=0; s[j]!='\0' ; j++)
		{
			printf("s[%d] = %c || s2[%d] = %c || c = %d\n",i,s[i],j,s2[j],c);
			if (s2[j]==s[i] )
			{  
				printf("%s\n","if(s2[j]==s[i]) resolved to true");
				i++;
				if (s[i+1]==' ' || s[i]=='.' )
				{	
					printf("%s\n","if(s[i+1]==' ' || s[i]=='.') resolved to true");
					c++;
				}
				else
					break;
			}
			else
			{
				break;
			}
		}
	}



Output
Enter any string : the dog
s = the dog
enter word: dog
s2 = dog
s[0] = t || s2[0] = d || c = 0
s[1] = h || s2[0] = d || c = 0
s[2] = e || s2[0] = d || c = 0
s[3] =   || s2[0] = d || c = 0
s[4] = d || s2[0] = d || c = 0
if(s2[j]==s[i]) resolved to true
s[6] = g || s2[0] = d || c = 0
s[7] = 
 || s2[0] = d || c = 0


occurences are 1




I really can't guess what your solution is. Are you able to describe your plan to yourself in words? Until you can do that then trying to write the code to implement the plan will be next door to impossible. You could write code without a word plan for weeks and if you ever get to the answer it will probably be by luck and there is a very good chance you will never get to an answer. You need to describe a plan to yourself and try it on a piece of paper and see if the plan seems to work. Once you have that plan writing the code will be far easier.


BTW what is this about?
printf("\n\noccurences are %d",c+1);

Why are you adding 1 to c?
You will always get a false result (except by luck) with that in there.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 28 November 2010 - 05:11 AM

i know. i i realized all that.
the compiler stopped giving me results for other examples i tried.

i shall think more over this.
and yes, i do have a psuedocode. though i keep spotting loopholes in it so i keep modifying it.
i noticed the +1 aswell. i've been at this for days, i don't even remember now. i shall overlook this step by step.

this is proving harder than i thought. i made 8 programs which seemed harder than this, in 45 minutes yesterday. this seems to be taking forever.
Was This Post Helpful? 0

#12 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 30 November 2010 - 05:10 PM

PLEASE HELP ME!!!
I can't seem to solve this. i have my finals in 2 weeks and for the past 3 days i have been doing nothing but this. and now tomorrow is the due date for the submission of this program.
PLEASE help me!!
i will be truly grateful.
Was This Post Helpful? 0

#13 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 30 November 2010 - 05:14 PM

my most recent program.
it doesn't work either.
especially for example:

this is island.


   #include <stdio.h>
#include<iostream.h>
#include <conio.h>

main()

{
     cout<<"Enter any string : "<<endl;
     char s[50];
     int c=0;

	 char s2[20];

	 gets(s);
	 cout<<"enter word"<<endl;
	 gets(s2);

	 for (int i=0; i!='\0' ; i++)
	 {
		 for ( int y=0; y!='\0'; y++)
		 { 
			


			while (1)
			{
				if (s[y]==' ')
							
				{
					
			
				 y++;
				 while (s[y]==s2[i])
				 {
					
					 if( (s[y+1]== ' '|| s[y+1]=='\0') && s2[i]=='\0')
					 { 
						 c++;
					 }
					
					 i++;
					 y++;
				 }

				}
				
				else
				{	i=0;
				break;
				}

				

				 
			}
	 }}
				
				 



cout<<c;


} 

Was This Post Helpful? 0

#14 Guest_silver90*


Reputation:

Re: Find a Word in a String Without Find Function

Posted 30 November 2010 - 05:39 PM

and finding the no of occurence of 'is' as 1
Was This Post Helpful? 0

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Find a Word in a String Without Find Function

Posted 01 December 2010 - 01:06 AM

Good code layout makes it far easier to write correct code.
A good cook has a clean and well organised kitchen. You need to have a clean and well organised code layout if you are going to see easily what you have written and where you have made a mistake. You will also find that you may find people refuse to help you if your code is so badly laid out that they have to struggle to read it. You code is bad enough that people will ignore it. Be better than that.

So choose one of the common indentation styles and use it consistently.
http://en.wikipedia....ki/Indent_style

Just like before add some debug outputs to help you see what is happening in your code.
Put this line in where indicated
if (s[y]==' ')
{
	cout << "DEBUG: if (s[y]==' ') resolved to true" << endl;

	y++;


How often do you pass that if() test?

I still have no idea at all what solution you are trying to implement so it is next door to impossible to help you because it looks like you are just throwing random code at the page. You said before you have a plan. Have you tried that plan on a piece of paper with a pencil to see if it can possibly work? It doesn't appear that you have based on what you are presenting to us.

And at some stage your code has changed from C to C++. (Although a 10+ year out of date version of C++ that should not be seen in any civilised society.)

This is just a big bucket of mystery.

BTW
You should never use gets(). Never. Never. Never. I hope I am being clear. NEVER!!
Read this
http://www.gidnetwork.com/b-56.html
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2