I am currently trying to write a program that:

Write a program to count and print out the number of copies of the letter 'e' that are found in a pre-specified string. (By "pre-specified", I mean you should declare and initialize the string right in your program, rather than reading it in from the command-line.) The count should be accurate regardless of what the string is defined to be. Add a brief comment at the beginning of the file describing what the program does.
The program output should look like:

In the following string:

"You may put any string you choose here"

The letter 'e' appears 3 times

I am having lots of trouble with this as I don't even know where to begin. This is what I have so far:

int main ( )
{
 	
	
	char sentence [] = "This is my sentence for the nume.c part of the lab";
	int ecount = e;
	

	printf ("In the following string:\n");
	printf ("  %s\n", sentence);
	   
	
	return(0);
}

I just don't know where to go from there. I was told that it would be a good idea to make the letter you are searching for be #defined as a constant.

I am a beginner in C and I am just learning, so please try to explain it in a way I will understand.

Recommended Answers

All 3 Replies

Do you know about for loops, and how to use them to index elements of an array?

Try using a loop to print out each letter of the char array in turn. From there it's a small jump to counting specific letters.

I know how to loop numbers so they go in order or by evens and odds and stuff, but not in way to make a sentence. Can you give me a few lines of example for it? I just need a point in the right direction.

Can you give me a few lines of example for it? I just need a point in the right direction.

A few lines is all that is needed, and I am afraid that would rob from you the experience of learning.
Look at this and give it another try.

integer e_counter = 0
integer index = 0


Loop starts index at 0; continue until string[ index ] = '\0'; increase index by one.
    if string[ index ] is e
        add one to e_counter
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.