Please help~ i stuck in this question so long time... This assignment is need to hand up due tomorrow >.<''' . Once again i confuse about array. Please correct me and show me some tips ya. Any of ur attention will be 'God Bless You'.
Here is my coding>>>

#include<stdio.h>

void main()
{
	char str[51];
	char letter[27]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
	int count, i;

	printf("Enter sentence or phrase>>>");
	fgets(str, 51, stdin);
	for(count=0;str[count]!='\0';count++)
	{
		for(i=0; letter[i]; i++)
			if(letter[i]==str[count])
				letter[i]++;
	}
if(letter[i]==str[count])

Think about what you are doing here. You're saying something to the effect of, "if the i-th element of letter[] equals the count-th element of str[] ...". Since you've initialized letter[] to 'a', 'b', ... this works the first time. But then you go and increment letter. So, say the letter was a 'b'. letter[] now contains 'a', 'c', 'c', 'd', .... Likely not what you wanted.

Your trouble is that you need a separate array to count the occurrences of the letters. You can't just increment letter, because letter[] is your lookup array. Its contents can't change.

So I suggest that you create another array, say:

int num[27] = {0};

The ={0} code just initializes each element of num[] to zero, which is what you want, because you can then start counting some letters.

This would work, but you don't need the letter[] array at all. But that's another post.

Thx dwks. Can you show further the algorithm of this program for me to work on? :D

Something like this. I've glossed over details that you seem to already understand.

  • Declare an array to hold the letter counts one element for each letter, with each element initialized to zero.
  • Read in a string from the user.
  • For every letter in this string:
    • If a character in the string is a letter:
      • Increment the corresponding element in the letter count array.

Okay, so that was a little difficult to read. Let me try that again.

The easiest way to do this is probably to count the number of occurances of every character you could possibly come across. For example:

int num[256] = {0}, count;

for(count=0;str[count]!='\0';count++)
{
    num[str[count]] ++;
}

Then, extract the counts that you care about. For this, use the function isalpha() from <ctype.h>, which returns true if its argument is an alphabetic letter, lowercase or uppercase. (There's also islower() and isupper() for lowercase and uppercase letters only.)

Then, if the count you're interested in is the count of a letter, print it out.

for(x = 0; x < 256; x ++) {
    if(isalpha(x)) {
        /* count[x] is a count of a letter of some sort */
    }
}

You might also want to only handle the counts that are non-zero.

Sorry i never learn about <ctype.h>, isalpha, islower and isupper, is there other way??? My teacher probably wouldn't accept those. And about this statement, i not really get it >>num[str[count]] ++;<<
Thx for help.

if (islower(x)) can be written if (x >= 'a' and x <= 'z')

Never define main as void return. Start defining main as int main( void ) or int main ( int argc, char *argv[] ) if you are expecting arguments from the command line.

>Sorry i never learn about <ctype.h>, isalpha, islower and isupper, is there other way???

#include <stdio.h>
/* #define ALL_CHARACTER */ /* enable for use all characters in RANGE */
/* #define RANGE */  /* enable for only lower case */
#ifdef RANGE
#define START 'a'
#endif
#ifndef RANGE
#define START 'A'
#endif

int main( void )
{     
    char character[256] = { 0 };
    int c = '\0';
    
    puts( "Enter some text:" );
    
    while ( ( c = getchar() ) != '\n' && c != EOF )
    {
        ++character[c];
    }
    for ( c = START; c <= 'z'; c++ )
    {
#ifndef ALL_CHARACTER
        if ( character[c] )
#endif
            printf( "%c = %d\n", c, character[c] );
    }
    getchar();
    return 0;
}

Cannot you use std::string apis ?

Member Avatar for iamthwee

>Cannot you use std::string apis ?
This is not the c++ forum.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
int main( void )
{
   int lettercount[26] = {0};
   char letterz[] = {"abcdefghijklmnopqrstuvwxyz"};

   char text[100];
   fputs("enter some text: ", stdout);
   fflush(stdout);
   if ( fgets(text, sizeof text, stdin) != NULL )
   {
      char *newline = strchr(text, '\n'); /* search for newline character */
      if ( newline != NULL )
      {
         *newline = '\0'; /* overwrite trailing newline */
      }
      printf("text = \"%s\"\n", text);
      
      int i;
      int j;
      int wordLength = strlen( text ); /*get the number of characters in the sentence*/
      for ( i = 0; i < wordLength; i++ )
      {
        for ( j = 0; j < 26; j++ )
        {
          if ( tolower( text[i] ) == letterz[j] ) /*tolower convert uppercase to lowercase*/
          {
            lettercount[j]++;
          }
        }
      }
   }
   
/*print out results*/
   int k;
   for ( k = 0; k < 26; k++ )
   {
     printf( "%c:%d ", letterz[k], lettercount[k] );
     printf("\n");
   }
   return 0;
}

Pay attention to the following tutorial about reliably getting input from the user:
http://www.daniweb.com/tutorials/tutorial45806.html

if (islower(x)) can be written if (x >= 'a' and x <= 'z')

Okay i got it. Thankyou :)

Never define main as void return. Start defining main as int main( void ) or int main ( int argc, char *argv[] ) if you are expecting arguments from the command line.

>Sorry i never learn about <ctype.h>, isalpha, islower and isupper, is there other way???

#include <stdio.h>
/* #define ALL_CHARACTER */ /* enable for use all characters in RANGE */
/* #define RANGE */  /* enable for only lower case */
#ifdef RANGE
#define START 'a'
#endif
#ifndef RANGE
#define START 'A'
#endif

int main( void )
{     
    char character[256] = { 0 };
    int c = '\0';
    
    puts( "Enter some text:" );
    
    while ( ( c = getchar() ) != '\n' && c != EOF )
    {
        ++character[c];
    }
    for ( c = START; c <= 'z'; c++ )
    {
#ifndef ALL_CHARACTER
        if ( character[c] )
#endif
            printf( "%c = %d\n", c, character[c] );
    }
    getchar();
    return 0;
}

Aiks, i really fresh in C. Your coding contain some element i never learn before. haha:confused: But thx, i copy paste the code and run it, it work perfectly.:)

Cannot you use std::string apis ?

i never learn about it. :$ haha

Aiks, i really fresh in C. Your coding contain some element i never learn before.

Learning is a process always on going.

i copy paste the code and run it, it work perfectly.:)

No copy and paste. Write it. Practice. Write it again. Practice again.
You'll learn nothing by just coping and paste and looking at it, and rejoicing that it works.
You don't know that it works until you understand why it does.

Cannot you use std::string apis ?
>i never learn about it. :$ haha

That's not C. Don't pay attention to it.

Yosh, Thank you all who reply my thread. Problem solved. :)
Thx alot ya.

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.