BleepingComputer.com: simple prog to seach no of occurences of word

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

simple prog to seach no of occurences of word

#1 User is offline   ayush 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 19
  • Joined: 09-August 09

Posted 25 November 2010 - 12:07 PM

well i made a prog for this.... so can anybody please help me.. in finding the error...... the ans given by my prog is 0

well here goes the code
#include<stdio.h>
main()
{
char a[20],b[5],c[20];
int i=0,j=0,k=0,count=0;
printf("Enter the string u want to search in");
gets(a);
printf("enter the word u want to search");
gets( b );
while(i!=strlen(a)-1)
{
	if(a[i]==' ')
	{	for(j=k;j<=i-1;j++)
		{c[j-k]=a[j];}
	c[j+1]="\0";
	k=j+2;
if (strcmp(b,c)==0)
{
count++;
} 
	}
i++;
}
printf("%d",count);
}

This post has been edited by groovicus: 26 November 2010 - 08:54 AM


#2 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,834
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

Posted 27 November 2010 - 12:30 AM

1. how about using strncpy instead of the for loop? makes things easier and clear
2. you are not searching for the last word because it has no space after it.

#3 User is offline   ayush 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 19
  • Joined: 09-August 09

Posted 28 November 2010 - 08:08 AM

thanx for the reply..
but does that mean.. logically my prog. is correct(if i use for loop to extract the word and then to compare it with other string) coz this method works in vb 6.0 as far as i remember.... but in this case the answer is coming 0
any idea regarding it

ps....ok thanx for pointing out the second problem... i totally forgot abt that....


ok i had one more doubt regarding this...
use of functions are better or looping is better when we consider program execution speed and size of the program

thanx again

#4 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 28 November 2010 - 09:58 AM

Even though you are making a function call, strncpy is still using a loop.

Quote

use of functions are better or looping is better

What do you mean by 'better'?
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,834
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

Posted 28 November 2010 - 10:23 AM

In C, you can use functions like strstr() to find the occurrence of a string.

In your program, include string.h before you use string functions.
Look how strstr() function is used : http://msdn.microsoft.com/en-us/library/z9da80kz%28VS.71%29.aspx

Why you are getting 0? Because you are not able to extract occurrence of search word in c variable. Check your for loop. Just put printf(%s\n", c); to see what you are extracting.

This post has been edited by Romeo29: 28 November 2010 - 10:28 AM


#6 User is offline   ayush 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 19
  • Joined: 09-August 09

Posted 29 November 2010 - 08:06 AM

View Postgroovicus, on 28 November 2010 - 09:58 AM, said:

Even though you are making a function call, strncpy is still using a loop.

Quote

use of functions are better or looping is better

What do you mean by 'better'?


better in terms of efficiency and execution time.... coz in bigger programs execution time matters

#7 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 29 November 2010 - 08:37 AM

Program execution time is measured in how fast it can process data, and unless you are handling huge amounts of data, one program construct is not going to be any faster than another. The program that you are trying to write is not going to be any different if you do it your way, or if you take Romeo's advice and do it his way. A function is just code, so there is no guarantee that it is going to be faster. In most cases, standard libraries are written for general use; meaning that in most instances they are going to be very efficient. There will be edge cases in which they are not efficient though. I had one professor that refused to use built-in sorting functions because they were not efficient enough for him. He just wrote his own.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#8 User is offline   ayush 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 19
  • Joined: 09-August 09

Posted 29 November 2010 - 11:41 AM

ohhh yeah i got it

garbage value in c array hd some garbage value and that was creating problem... so i just had to clear it

@ groovicus :ahhh that means suppose if i have a program of creating n operating system(suppose) which require both frontend coding as well as backend coding and thus requiring a large amount of data.... in that case (from execution speed point of view )creating my functions from the scratch is better option or to use built in function...(suppose in this case we forget about the time we require in coding and the entire team of it follows the same methodology of programming from scratch)....????

thank you everyone for solving my doubt ...

#9 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 29 November 2010 - 03:02 PM

Quote

creating my functions from the scratch is better option or to use built in function

Maybe.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users