i am trying to take a input from user ,
suppose if user types

"a bat and a ball and stick"

if my array is

data[10][20][30] = { 
                               
                               {"a","and","apple"},
                               {"bat","ball"}
                             
                                        };

from the sentence how i can count

occurrence of "a" in the sentence ( ie , from users input) = 2;
occurrence of "bat" in the sentence = 1;
occurrence of "and" in the sentence = 2;
occurrence of "ball" in the sentence = 1;

and how to store this count ,

and using these counts , how i can i print the sentence in screen from the array . please...

I'm assuming this a programming assignment from school...If you need help with homework, you have to show some effort and tell us what you've tried. But it seems like you don't know where to start so I'll give you a few pointers (may not be the most efficient method, but it will work):

- Use a char array that will hold the sentence inputted by the user, for instance:
char sentence[LEN + 1];

- LEN is a variable that is defined under the #includes.... so for example, you should have something like:

#include <stdio.h>
#include <stdlib.h>

#define LEN 255 //LEN is the buffer length, you can change the 255

- To read the sentence, you should use the fgets() function...So a line of code in C that will extract the sentence from the user and store all the words in separate indices is:

fgets(sentence, sizeof(sentence), stdin);

- You can use two arrays, one to hold the words of the sentence and one array to keep track of the number of occurrences of each word. For instance, you can make an array for number of occurrences:

int occurrences[NUM];

- You can use dynamically allocated arrays, but I'm pretty sure your teacher isn't going to make you do that...

- Make each word stored in the sentence array corresponds to a a number in the number of occurrences array.

- Another strategy you might want to do is while you scan the words of the user input, make sure the word it scans isn't already in the array. If it's already in the array, just increment the number in the corresponding number of occurrences array. If it's not in the array already, create a new array entry for it...

- When you finally print out the results, you will need a loop to traverse both arrays...Use a for loop to traverse the indexes of both arrays and print them out as you're iterating through them...

This project is fairly simple, but I hope this "advice" helps...If it doesn't, post code on what you've tried and we'll help...

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

void main()
{

int index,result;
char *typed;

int maxcount[10] ;
char data[10][10][30] =   {

			                   { "A","Apple","And","Adobe"}
                                     };


clrscr();

printf("enter");
gets(typed);

maxcount[0] =3 ;

	for(index=0;index<=maxcount[0];index++)
	{

		result = strcmp(typed,data[0][index]);

		if(!result)
		{
		  cprintf(" Found %s",data[0][index]);
		  break;
		}

	}


getch();
}

suppose this my program to match the userinput with arrays and if found print them from the array ,
suppose if user enter
"A Apple And A Apple" // imagine .....

how to count the number of occurrence and position of a string how to store that , and then print the user input using that..

>#include<conio.h>
I recommend you forget this header exists until you learn why it's a bad idea.

>void main()
main returns int.

>char *typed;
>gets(typed);
That's just brilliant. Not only did you fail to allocate memory to typed (who knows where those characters are being written), you chose to use gets despite the fact that it's impossible to use safely and ultimately one huge ugly wart on the face of C. Please use fgets instead.

Anyway, I'm not sure what Compton11 was thinking with his advice, but parsing a string makes this program more difficult. You can use scanf to read individual words:

#include <stdio.h>

int main ( void )
{
  char word[256];

  while ( scanf ( "%255s", word ) == 1 )
    puts ( word );

  return 0;
}

From there it's simply a matter of maintaining a list of words and the corresponding frequency. For a relatively short list of words, you can easily use two parallel arrays for this.

>char *typed;
>gets(typed);
That's just brilliant. Not only did you fail to allocate memory to typed (who knows where those characters are being written), you chose to use gets despite the fact that it's impossible to use safely and ultimately one huge ugly wart on the face of C. Please use fgets instead.

It is not the first time that [s]he has been told; it would not be the last time neither, I am afraid.

hi ,friends i tried alot , i got idea , but i don't know how to implement programmatically. thats why, i am requesting .Please help me.

some one is trying to give some new ideas , that is don;t use gets . But no one give any solutions .....


if user enters

"a apple and a apple and apple and apple"

this sentence contains 8 words . compare each word with the array. if found

1)keep track of the position
2)count of that word ( occurrence) in the sentence.
3) and print the sentence using the position and count arrays /// relate these count and position with the array data[10][10][30] and print the sentence from the array

please.....

>Please help me.
Give me a good reason why I should. I've already helped you more than you deserve, and you ignored me completely.

>But no one give any solutions
That's because someone[1] isn't comprehending the replies. Not only did I give you a superior alternative to gets with a working example (thus solving half your problem), I described an easy way to solve the other half. I'm not going to do it all for you, so engage your brain and try again.

[1] That someone is you, vijaysoft1, in case you were wondering.

Hi naru , be clear my problem is

how to store the position and count of particular word into another arrays.

plz i want to make clear

and i don;t want the full program from u.

plz give this only....

>how to store the position and count of particular word into another arrays.
Yes, let's be clear. Do you need the position of a word in a string for any other reason than to find the count of that particular word? Or is that simply an implementation detail?

If you really do need it, and you're not just regurgitating Compton11's proposed solution, you need to start over with a full description of the requirements, because that requirement was absent in your original request.

If you don't need it, I'll continue to recommend you forget that particular solution and use scanf to separate the words. That way you can more easily store and count them.

>and i don;t want the full program from u.
If I give you any more code, the problem will be solved. That's how easy it is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.