•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,570 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,604 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 2859 | Replies: 13 | Solved
![]() |
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>>>
Here is my coding>>>
c Syntax (Toggle Plain Text)
#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])
Your trouble is that you need a separate array to count the occurrences of the letters. You can't just increment letter[i], because letter[] is your lookup array. Its contents can't change.
So I suggest that you create another array, say:
int num[27] = {0};This would work, but you don't need the letter[] array at all. But that's another post.
dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Something like this. I've glossed over details that you seem to already understand.
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:
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.
You might also want to only handle the counts that are non-zero.
- 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.
- If a character in the string is a letter:
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, 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 */
}
} dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
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???
>Sorry i never learn about <ctype.h>, isalpha, islower and isupper, is there other way???
C Syntax (Toggle Plain Text)
#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; }
Last edited by Aia : Oct 21st, 2007 at 1:02 am.
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
•
•
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,018
Reputation:
Rep Power: 6
Solved Threads: 68
Cannot you use std::string apis ?
>Cannot you use std::string apis ?
This is not the c++ forum.
Pay attention to the following tutorial about reliably getting input from the user:
http://www.daniweb.com/tutorials/tutorial45806.html
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
Last edited by iamthwee : Oct 21st, 2007 at 11:31 am.
... the hat of 'is this a cat in a hat?'
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Program- calculate # of days between two days (C++)
- java program to calculate simple interest (Java)
- Using Loops to calculate totals in C Program (C)
- Can anyone suggest why this program doesn't calculate correctly? (Java)
- Need help with C program (C)
- sentence capitalizer (C++)
Other Threads in the C Forum
- Previous Thread: c Compiler (Not C++)
- Next Thread: binary



Linear Mode