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.

Dani AI

Generated

A simple, reliable approach is: pick a target character, walk the string one character at a time until the null terminator, and increment a counter whenever the current character matches the target. That handles any pre-defined string and works for any letter you choose. Using a named constant for the target (a #define or const char) makes the code clearer and easier to change.

, the line int ecount = e; causes a compile error because e is not defined; counters must be initialized to a number (usually zero) and characters are compared with single quotes, e.g. 'e'. 's hint about indexing with a loop is exactly the right direction, and 's sketch of looping until the '\0' character is the core idea — below is a concise C example that implements those ideas.

#include <stdio.h>

#define TARGET 'e'

int main(void)
{
    const char sentence[] = "You may put any string you choose here";
    int count = 0;
    size_t i;

    for (i = 0; sentence[i] != '\0'; ++i)
        if (sentence[i] == TARGET)
            ++count;

    printf("In the following string:\n\n\"%s\"\n\nThe letter '%c' appears %d times\n",
           sentence, TARGET, count);

    return 0;
}

Troubleshooting tips: compile with warnings (gcc -Wall -Wextra) to catch mistakes. Common beginner errors include using double quotes ("e") instead of single quotes ('e'), not initializing the counter, and trying to compare strings with ==. To count both lowercase and uppercase e, either test both (sentence[i] == 'e' || sentence[i] == 'E') or use tolower from <ctype.h> after casting to unsigned char.

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.