Sunday, July 12, 2009

C programming?

How to read integers from user input and convert them to morse code?

C programming?
I'm not saying this is the best way, but it's the easiest. It's late, so I might have a bug. Double check this. The best way would probably to create an array of structures each with the character and the morse string. There is a way to represent morse as binary, but if you can't figure it out it's probably best to start with strings.





int main(int argc, char *argv[]){


char buf[200], *pointer;





char *morseint[10] = {"-----", /* 0 */


".----",


"..---",


"...--",


"....-",


".....",


"-....",


"--...",


"---..",


"----." /* 9 */};





fgets(buf,sizeof(buf),stdin);





for(pointer= buf; *pointer; pointer++){/* transverse the entire string*/


if(!isdigit(*pointer)) continue;


/*maybe print an error You said integers*/


printf("%s ", morseint[*pointer-'0']);


/* get the character and subtract 0 from it and use that value as the index to the array you just created.*/


}


return 0;


}


No comments:

Post a Comment