Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

# simple prog to seach no of occurences of word

8 replies to this topic

### #1 ayush

ayush

• Members
• 19 posts
• OFFLINE
•
• Local time:01:08 PM

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);
}
```

Edited by groovicus, 26 November 2010 - 08:54 AM.

### #2 Romeo29

Romeo29

Learning To Bleep

• Members
• 3,194 posts
• OFFLINE
•
• Gender:Not Telling
• Location:127.0.0.1
• Local time:02:38 AM

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 ayush

ayush
• Topic Starter

• Members
• 19 posts
• OFFLINE
•
• Local time:01:08 PM

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 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:01:38 AM

Posted 28 November 2010 - 09:58 AM

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

use of functions are better or looping is better

What do you mean by 'better'?

### #5 Romeo29

Romeo29

Learning To Bleep

• Members
• 3,194 posts
• OFFLINE
•
• Gender:Not Telling
• Location:127.0.0.1
• Local time:02:38 AM

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.

Edited by Romeo29, 28 November 2010 - 10:28 AM.

### #6 ayush

ayush
• Topic Starter

• Members
• 19 posts
• OFFLINE
•
• Local time:01:08 PM

Posted 29 November 2010 - 08:06 AM

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

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 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:01:38 AM

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.

### #8 ayush

ayush
• Topic Starter

• Members
• 19 posts
• OFFLINE
•
• Local time:01:08 PM

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 groovicus

groovicus

• Security Colleague
• 9,963 posts
• OFFLINE
•
• Gender:Male
• Location:Centerville, SD
• Local time:01:38 AM

Posted 29 November 2010 - 03:02 PM

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

Maybe.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users