Count how many times alphabet letter appear in a string

Count how many times every alphabet letter appear in a string

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 14055 Views - Last Post: 25 October 2010 - 06:58 PM Rate Topic: -----

#1 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 06:18 AM

Good day everone... Im having a little problem here. I figured out the main problem but im wondering what should i change. This is my first post. Pls forgive me if ever i have done any improper grammar and spelling.
So here is the code that I've tried. I tried my best though.
#include <stdio.h>
#include <conio.h>
#include <string.h>

void main(void)
{
	char string[20];
	char letters[27] = "aabcdefghijklmnopqrstuvwxyz", letter;
	int count[27] = {0};
	int i = 0, a = 0, b = 0, c = 0;
	clrscr();

	printf("Enter a string: ");
	gets(string);

	while(letters[c] != '\0')
	{
		if(letters[c] == string[i])
		{
			count[b]++;
			i++;
			c = 0;
		}
		b++;
		c++;;
	}

	for ( a = 0; a <= 25; a++ )
	printf("\t%d\n", count[a]);

	gotoxy(1,2);
	for ( letter = 'a'; letter <= 'z'; letter++ )
	{
		printf(" %c\n", letter);
	}

	getch();
}



So here is my desired output hehehe...
Example:

Enter a string: hello
a = 0
b = 0
c = 0
d = 0
e = 1
f = 0
g = 0
h = 1
i = 0
j = 0
k = 0
l = 2
m = 0
n = 0
o = 0
...

Pls help me heheheh ... I will deeply appreciate your help.. Ive been trying these for many days ... :(

Is This A Good Question/Topic? 1
  • +

Replies To: Count how many times alphabet letter appear in a string

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 06:23 AM

I have a blog entry on this that might prove useful.

Note well that using gets() is very bad; here is a much better option. I'm guessing however that's part of your education, which probably also requires you to use Turbo C, a 20-year-old compiler. It's a real shame. You're being cheated by your teachers and educational system to have to learn old technology.
Was This Post Helpful? 0
  • +
  • -

#3 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 06:36 AM

Mr. JackOfAllTrades,

Thank you for your fast reply. But I cannot understand the code that you were trying to give to me. But pls. fix my code :(. Our professor will mark my testpaper as wrong. She did not want to see any codes that was not behind her lessons. Pls understand my situation although our school live in an old field of education. They treat turbo c as a basic foundation and our professor did not allow us to use any other compiler rather than Turbo C. Pls sir. Thank you for your consideration.
Was This Post Helpful? 0
  • +
  • -

#4 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 06:48 AM

You have two 'a''s in your letters array, presumably to make 'a' be in position 1, yet later you use 0 based indexing.

Think about the algorithm to count letters in a sentence:
Look at the current letter in the sentence
Tally the letter you see
Move on to the next letter in the sentence



The main problem you are facing is the logic in your while(letters[c] != '\0') loop. You should create a function to give you the index in count[] you should increase based on the current letter. You have no reason to set c to 0 inside the loop. b is incremented incorrectly, but if you create a function to return the index, b can be removed.

It would be interesting for you to know more about look-up tables... I will explain the best solution for this problem when you have come up with working code.

I just saw your latest post, have you covered functions yet?

This post has been edited by oscode: 25 October 2010 - 06:51 AM

Was This Post Helpful? 1
  • +
  • -

#5 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 02:38 PM

Ohhh.. yeah .. we have covered functions. so ill be returning the c index?... uhmmm.. ill try first and then ill post again hehehhe... Thank you for your reply. I appreciate it.... Thank you Sir!....
Was This Post Helpful? 0
  • +
  • -

#6 geofbot  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 03:10 PM

Try a for loop looping from a to z while another for loop within that for loop loops through the string entered for that character. Sorry if that was confusing. In other words, for loop looping from a to z, a for loop inside that loop goes through the string checking for the letter (a to z).

GEOFBOT

BTW, welcome to </dream.in.code>!

NOTE: I'm not really a C programmer, but I'm learning c++, so I guess that my idea is valid c.

This post has been edited by geofbot: 25 October 2010 - 03:11 PM

Was This Post Helpful? 1
  • +
  • -

#7 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 03:25 PM

hmmm is is something like this?


char string[20];
for ( letter = 'a'; letter <= 'z'; letter++ )
{
     for ( counter = 0; counter <= '\0'; counter++ )
     {
          if ( string[counter] == letter )
                counter++; //I think i should have another counter for this? 
     }
}




how can i have a counter from a -z? should i still use the int array from 1- 26?
Was This Post Helpful? 0
  • +
  • -

#8 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 03:38 PM

Pls help me anyone...
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,627
  • Joined: 16-October 07

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 03:51 PM

char string[20];
// we'll assume you put a value in string somewhere before this
// I guess you declare letter and counter somewhere else?

// this letter loop isn't bad
for ( letter = 'a'; letter <= 'z'; letter++ ) {
	// counter is int?  
	// what does counter <= '\0' mean?
	// when will it ever be true?
	for ( counter = 0; counter <= '\0'; counter++ ) {
		// ok... this bit could work
		if ( string[counter] == letter ) {
			// yes!  you should have another counter here!
			// ouch
			// counter++; //I think i should have another counter for this? 
			// you'll probably what to declare this just inside the letter loop
			letterCounter++;
		}
	}
	// you'll want to print here, yes?
	// the letter and the letterCounter
}


Was This Post Helpful? 2
  • +
  • -

#10 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 04:00 PM

ohhh thank you ... i ll try to fix it heheheh....
Was This Post Helpful? 0
  • +
  • -

#11 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 04:38 PM

#include <stdio.h>
#include <conio.h>
#include <string.h>
// do you mean i have to create a function and put the declaration there?

void main(void)
{
	char string[20] = "abcd"; // i just want to check if it will work...
	char letter;
	int letterCounter[26] = {0};
	int counter = 0;
	clrscr();

	for ( counter = 0; counter < 26; counter++ )
	{
		for ( letter = 'a'; letter <= 'z'; letter++ )
		{
			if (string[counter] == letter)
				letterCounter[counter]++;
		}
		printf(" %c = %d\n", letter, letterCounter[counter]);
        // it prints { { { and gives 1111 to abcd and 26 to the letter somewhere in w.. i wonder why... 
	}

	getch();

}

]

Sorry if i wasn't able to follow .... Its just it cannot sink into my mind.. Im still trying to improve. pls give me more help ... more powers in dreamincode.net!.... :(
Was This Post Helpful? 0
  • +
  • -

#12 geofbot  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 04:45 PM

Isn't the
printf(" %c = %d\n", letter, letterCounter[counter]); 

supposed to be in the for loop
        for ( letter = 'a'; letter <= 'z'; letter++ )  

         {  

             if (string[counter] == letter)  

                 letterCounter[counter]++;  

         }  


?

I'm not sure, but by the looks of it, it seems so.
Was This Post Helpful? 0
  • +
  • -

#13 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 05:14 PM

like this sir?
for ( counter = 0; counter < 26; counter++ )
    {
        for ( letter = 'a'; letter <= 'z'; letter++ )
        {
            if (string[counter] == letter)
                letterCounter[counter]++;
                printf(" %c = %d\n", letter, letterCounter[counter]);
                // should i place it here?
        }

        
    }



wew.... i still have to eat many codes to be a programmer hehhehe.. pls give me more clues.. :( heheehhe

sir the output was wrong.... what part of the code should i fix? do i have to fix all throughout my code? what certain changes do i have to make?

i tried to move it but im not sure if i placed it right. . but the output was wrong... it keeps on printing a, b, c, d, ... with a value of 0. Pls help me more heheheh...

i tried to make { } inside the loop but it displays twice like if input was ff.. it prints f = 1, f = 1, then a-z as 0 value... i wonder :(
#include <stdio.h>
#include <conio.h>
#include <string.h>
// do you mean i have to create a function and put the declaration there?

void main(void)
{
	char string[20] = "abcdeffzzz";
	char letter;
	int letterCounter[26] = {0};
	int counter = 0;
	clrscr();

	for ( counter = 0; counter < 26; counter++ )
	{
		for ( letter = 'a'; letter <= 'z'; letter++ )
		{
			if (string[counter] == letter)
			{
				letterCounter[counter]++;
				printf(" %c = %d\n", letter, letterCounter[counter]);
			}
		}

	}
	getch();

}



Anybody there? Pls help me fix this one :( Pls :(
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5795
  • View blog
  • Posts: 12,627
  • Joined: 16-October 07

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 05:23 PM

Use an array, or use the last setup you showed. Don't do both.

While your non array version isn't the most efficient, I kind of like it. It really takes very little to fix it, but I'm afraid if I try to explain it, I'll only confuse the issue more.

The following code works. Please make sure you understand it.

#include <stdio.h>

void showCount(const char *str) {
	char letter;
	for ( letter = 'a'; letter <= 'z'; letter++ ) {
		int i, letterCounter = 0; // init your counter
		for (i=0; str[i] != '\0'; i++ ) { // loop through the string
			if ( str[i] == letter ) { // if the letter in the string == letter, count it
				letterCounter++;
			}
		}
		if (letterCounter>0) { // don't bother showing the zeros
			printf("%c = %d\n", letter, letterCounter);
		}
	}
}

int main() {
	showCount("this is only a test");
	return 0;
}



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

#15 geofbot  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 80
  • Joined: 26-September 10

Re: Count how many times alphabet letter appear in a string

Posted 25 October 2010 - 05:23 PM

Actually, I would place it after the if, but still inside the for, like this:
for ( counter = 0; counter < 26; counter++ )
    {
        for ( letter = 'a'; letter <= 'z'; letter++ )
        {
            if (string[counter] == letter)
                letterCounter[counter]++;
                
            printf(" %c = %d\n", letter, letterCounter[counter]);
        }

        
    }


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2