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.
#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.