Can someone help me solve a problem in C?

Suppose, you are given a composition of English words. You have to make a
program to show the character frequency of each sentence of this composition. Also you have to mention for which sentence you are showing the frequency of.

interesting problem. So you have to count characters and display this count in some way to relate it back to the sentence.
Have you written some psuedocode? I would happy to review it.

ps. And welcome to the forum Zarif!

That’s not just a C problem, that’s just an algorithm problem. What would the algorithm be in pseudo code?

/*This is the code I have written. This is to count total frequency of the composition. But I do not understand what to do for counting frequency of characters in each sentence separately. */

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

int main()
{
    char read;
    char s[10000];
    int  i=0,j,k,count=0,n;
    FILE *fp;

    fp=fopen("passage.txt","a");

    fclose(fp);

    fp=fopen("passage.txt","r");

        while(!feof(fp))
        {
            s[i] = fgetc(fp);
            i++;
        }

    fclose(fp);

    for(j=0;s[j];j++);
    {
        n=j;
    }


	printf("\n\t\t\tCharacter Counter of a paragraph from file\n");
	printf("\t\t\t__________________________________________\n\n");


    for(i=0;i<n;i++)
    {
     	count=1;
    	if(s[i])
    	{

 		  for(j=i+1;j<n;j++)
	      {

	        if(s[i]==s[j])
    	    {
                 count++;
                 s[j]='\0';
	     	}
	      }


	      if (s[i]==' ')
          {
             printf(" Number of words: %d \n",count);
          }
          if (s[i]=='.' || s[i]=='?' || s[i]=='!')
          {
             printf(" Number of sentences: %d \n",count);
          }
          printf(" '%c' = %d \n",s[i],count);

       }

 	}



    return 0;
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

I am new to the world of programming. It seems difficult for me at this moment. I have posted the code that I have written so far.

That code is confusing for me. It would be really helpful if you described in words what is the overall process you want the code to use.

If you had to give this task to a kid, what are the small steps you would tell them to use.

At first I am reading the composition from a file. Then I am copying the string to another. After that in a loop, I am checking equality. If it is equal, then counter variable increases and the character in the second string becomes null for avoiding repetition. Number of spaces counts the frequency of words and number of full stops, exclamation marks and question marks count the frequency of sentences.

What is this part for?

Checking equality of what?

I donh see how this is part of the instructions?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.