•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 455,967 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,741 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: 3013 | Replies: 58 | Solved
![]() |
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
Hello, i'm a noob C programmer, learning it at university. We have been given an assessment which i need to complete asap. I have figured out the solution to the porgram (almost) but need some assistace. I have a Java and VB background and i cannot seem to get my head around C & pointers!! i've been working on this problem for about 3 hours now. and i cant think any more. hence i cannot progress any further!!
Take a look at this.
i have to follow set criteria and code the solution using the arrays above. my method works fine. IT WORKS FINE for decoding one acharacter. But i need it to decode the whole string...
I'm having difficulty in looping through the entered string and returning the morse code.
please assist.
Take a look at this.
#include "stdafx.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"
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() {
char *ch;
printf("please enter char to convert to morse: ");
scanf_s("%s", &ch);
encode(ch);
return 0;
}
void encode(char *ch){
size_t i, j;
for ( i = 0; ch[i]; ++i ) {
for ( j = 0; j < sizeof ch; ++j ) {
if ( morsecode[(unsigned short)toupper(*ch) - alph[0]] ) {
printf("%c", morsecode);
break;
}
}
}
putchar('\n');
}
i have to follow set criteria and code the solution using the arrays above. my method works fine. IT WORKS FINE for decoding one acharacter. But i need it to decode the whole string...
I'm having difficulty in looping through the entered string and returning the morse code.
please assist.
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
yes. We're being taught ANSI C first. tehn moving onto C++.. this assignment is being coded by the requirements of the lecturer. i was given the array, and i must implement my solution arounf it. The object is to have a program which converts a user input AND is able to read from a file and coverts that to morse code.
As you can see.. i'm stuck on the loop for the string.
As you can see.. i'm stuck on the loop for the string.
Last edited by r5ingh : Nov 24th, 2007 at 1:05 pm.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
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 ch;
printf ( "please enter char to convert to morse: " );
scanf ( "%c", &ch );
encode ( ch );
getchar();
getchar();
}
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 1:10 pm.
... the hat of 'is this a cat in a hat?'
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
hmm thanks for the solution. deosnt print anything out though! loool!... i can see what i was trying to do wrong.
hmm. i have not used getchar() before. .... this isnt returning anything to the console. i dont think morsecode[i] is correct.? that said. im in no state to be right.
confused like shit at the moment.
hmm. i have not used getchar() before. .... this isnt returning anything to the console. i dont think morsecode[i] is correct.? that said. im in no state to be right.
confused like shit at the moment.
Last edited by r5ingh : Nov 24th, 2007 at 1:22 pm.
#include "stdafx.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"
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 ch;
printf ( "please enter char to convert to morse: " );
scanf_s ( "%c", &ch );
encode ( toupper(ch) );
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' );
}And now?
Last edited by iamthwee : Nov 24th, 2007 at 1:24 pm.
... the hat of 'is this a cat in a hat?'
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
nada. still not returning anything after the user input
hmm
printf ( "please enter char to convert to morse: " );
scanf_s ( "%c", &ch );
this should be
printf ( "please enter string to convert to morse: " );
scanf_s ( "%s", &ch );
the object is to encode the entrie string to morse one char at a time?
hmm
printf ( "please enter char to convert to morse: " );
scanf_s ( "%c", &ch );
this should be
printf ( "please enter string to convert to morse: " );
scanf_s ( "%s", &ch );
the object is to encode the entrie string to morse one char at a time?
Last edited by r5ingh : Nov 24th, 2007 at 1:28 pm.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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