该代码基本上采用用户输入(az)的内容并将其转换为莫尔斯电码。但是返回字符串最后总是有'm'试图做数百万的事情来修复它你能看到我做错了什么吗,谢谢。
//Functions
#include "stdafx.h"
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void morse(void); //prototype
int _tmain(int argc, _TCHAR* argv[])
{
for(;;){
morse(); // function call
}
}
void morse(void){
char *ss= (char*)malloc(110); // allocating dynamic memory
strcpy(ss, ".- -...-.-.-.. . ..-.--. ...... .----.- .-..-- -. --- .--.--.-.-. ... - ..- ...-.-- -..--.----..");
char *pp =(char*)malloc(110);
int i = 0;
int n = 0;
printf("Enter text to convert to morse code: ");
scanf("%s", pp);
char *q =(char*)malloc(110);
while (*pp != '\0'){//intiate while loop
*pp = *(pp + n);
int f = (*pp - 97)*4; //find letters postion in morse code string
int a = 0;
while (a != 4) //copy morse code for the letter into new string
{
*(q + i) = *(ss + f);
i++;
a++;
f++;
}
n++;
}
q[i] = '\0';
printf("%s", q); //return morse code
printf("\n");
free(ss); //free the allocated memory
free(pp);
free(q);
return;
}