•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 392,098 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,820 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:
Views: 2525 | Replies: 58 | Solved
![]() |
And this one...
void encode ( char ch );
char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* morsecode is an array of pointers to char. It is initialized
* so that each element points to a string literal with
* the ascii representation of each char of the array alph[]
*/
char *morsecode[] =
{ /* an array of pointers to char */
".- " , "-..." , "-.-." , "-.." , "." , /* a-e */
"..-." , "--." , "...." , ".." , ".---" , /* f-j */
"-.-" , ".-.." , "--" , "-." , "---" , /* k-o */
".--." , "--.-" , ".-." , "..." , "-" , /* p-t */
"..-" , "...-" , ".--" , "-..-" , "-.--" , /* u-y */
"--.." , /* z */
};
int main ( void )
{
char text[100];
fputs ( "enter some text to convert to morse: ", 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 j;
int foo = strlen(text);
for ( j = 0; j < foo; j++ )
{
encode ( toupper ( text[j] ) );
}
getchar();
getchar();
return 0;
}
void encode ( char ch )
{
size_t i, j;
for ( i = 0; i < 26; ++i )
{
if ( ch == alph[i] )
{
printf ( "%s", morsecode[i] );
break;
}
}
putchar ( '\n' );
} Last edited by iamthwee : Nov 24th, 2007 at 12:45 pm.
Member of: F-ugly code club
Join today don't delay!
Join today don't delay!
r5ing:
A few things:
» Don't use scanf or it's non-standard variants for accepting user input. There are better ways of achieving it. Read this.
» The way you expose the interface doesn't seem to be right. Make encode() return a char array instead of nothing.
» Also since Morse code is case insensitive you might consider converting the user input to uppercase before applying encode function to it.
How do you expect this to work? You are assigning the string entered by the user to something which doesn't belong to you. You need to allocate enough memory. Something like
A few things:
» Don't use scanf or it's non-standard variants for accepting user input. There are better ways of achieving it. Read this.
» The way you expose the interface doesn't seem to be right. Make encode() return a char array instead of nothing.
» Also since Morse code is case insensitive you might consider converting the user input to uppercase before applying encode function to it.
c Syntax (Toggle Plain Text)
> char *ch; > printf("please enter char to convert to morse: "); > scanf_s("%s", &ch);
char ch[BUFSIZ] = { 0 }; Plus scanf requires you pass the starting address of the memory location. Since ch itself is a char array you just need to pass ch instead of &ch. "I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
C Syntax (Toggle Plain Text)
void encode ( char ch ); char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; /* morsecode is an array of pointers to char. It is initialized * so that each element points to a string literal with * the ascii representation of each char of the array alph[] */ char *morsecode[] = { /* an array of pointers to char */ ".- " , "-..." , "-.-." , "-.." , "." , /* a-e */ "..-." , "--." , "...." , ".." , ".---" , /* f-j */ "-.-" , ".-.." , "--" , "-." , "---" , /* k-o */ ".--." , "--.-" , ".-." , "..." , "-" , /* p-t */ "..-" , "...-" , ".--" , "-..-" , "-.--" , /* u-y */ "--.." , /* z */ }; int main ( void ) { for (;;) { printf("################################################\n"); printf("## ##\n"); printf("## Main Menu ##\n"); printf("## ##\n"); printf("################################################\n"); printf("## Options: Type the option no: to proceed ##\n"); printf("## 1. Convert keyboard entry to morse code ##\n"); printf("## 2. Convert a file to morse code ##\n"); printf("## 3. Type Quit To Exit ##\n"); printf("################################################\n\n"); printf("## Enter an Option: ##\n"); } char text[150]; fputs ( "enter some text to convert to morse: ", 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 ); } putchar ( '\n' ); int j; for ( j = 0; j < strlen ( text ); j++ ) { encode ( toupper ( text[j] ) ); } getchar(); getchar(); return 0; } void encode ( char ch ) { size_t i; for ( i = 0; i < 26; ++i ) { if ( ch == alph[i] ) { printf ( "%s ", morsecode[i] ); break; } } }
I need to get it to be something like this. so that i can meet the minimum requirements of the assessment.
i am thinking of useing a switch case statement, to call the encode function and i will write a separate function for the file read method.
iamthwee: i will mark it as solved if i do not need further assistance! Thanks for your help so far. I REALLY appreciate it!
Last edited by r5ingh : Nov 24th, 2007 at 12:52 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- help needed on a loop again (C++)
- Square root program without sqrt or pwr (C++)
- IT project help (Pascal and Delphi)
- open a file and divide its data in arrays (C)
- C++ nest loop program - Help (C++)
- I don't know where is the question occured. (C)
- sorting parallel arrays (C)
Other Threads in the C Forum
- Previous Thread: HELP: Day 0 - Chroma Key Programming
- Next Thread: Pascal's Triangle in C



Linear Mode