这保持打印包含类似信件的所有SMS。
我需要运行代码而不考虑大小写
每次用户输入和短信,代码打印所有短信有关的信,例如,如果另一个短信有一个'a',它打印,加上原来的短信。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *
replaceWord(const char *string, const char *oldWord, const char *newWord)
{
char *result;
int i, cnt = 0;
int newWordlen = strlen(newWord);
int oldWordlen = strlen(oldWord);
for (i = 0; string[i] != '\0'; i++) {
if (strstr(&string[i], oldWord) == &string[i]) {
cnt++;
// Jumping to index after the old word.
i += oldWordlen - 1;
}
}
// Making new string of enough length
result = (char *) malloc(i + cnt * (newWordlen - oldWordlen) + 1);
i = 0;
while (*string) {
// compare the substring with the result
if (strstr(string, oldWord) == string) {
strcpy(&result[i], newWord);
i += newWordlen;
string += oldWordlen;
}
else
result[i++] = *string++;
}
result[i] = '\0';
return result;
}
char *
getMessage(char sms[25][5], char message[25][25], char *input)
{
int i = 0;
for (i = 0; i < 25; i++) {
if (strstr(input, sms[i]) != NULL) {
char *result = replaceWord(input, sms[i], message[i]);
input = result;
}
}
return input;
}
char *
getSMS(char sms[25][5], char message[25][25], char *input)
{
int i = 0;
for (i = 0; i < 30; i++) {
if (strstr(input, message[i]) != NULL) {
char *result = replaceWord(input, message[i], sms[i]);
input = result;
}
}
return input;
}
int
main(void)
{
char sms[25][5] = { "R", "OMG", "L8", "2DAY", "U", "2", "PLS", "PPL",
"GAS", "FTL", "GG", "WYA", "ETA", "WYD", "HBU", "WTM", "WYM", "LOL",
"L", "W", "TTYL", "SYL", "ILY", "BRB", "NRN" };
char message[25][25] = { "ARE", "OH MY GOD", "LATE", "TODAY", "YOU",
"TO", "PLEASE", "PEOPLE", "GREETINGS AND SALUTATIONS", "FOR THE LOSS",
"GOOD GAME", "WHERE YOU AT", "ESTAMATED TIME ARIVAL", "WHAT YOU DOING",
"HOW ABOUT YOU", "WHATS THE MOVE", "WHAT YOU MEAN", "LAUGH OUT LOUD",
"LOST", "WON", "TALK TO YOU LATER", "SEE YOU LATER", "I LOVE YOU",
"BE RIGHT BACK", "NOT RIGHT NOW" };
// int i=0;
int choice = 1;
char input[100];
while (choice != 3) {
printf("Please choose :\n"
"1: SMS to Message.\n"
"2: Message to SMS.\n"
"3:Exit\n");
scanf("%d", &choice);
fflush(stdin);
if (choice == 1) {
printf("Please give SMS :");
scanf("%[^\n]", input);
strcpy(input, getMessage(sms, message, input));
printf("Converting SMS to Message :%s\n", input);
}
else if (choice == 2) {
printf("Converting Message to SMS :\n");
scanf("\n%[^\n]", input);
strcpy(input, getSMS(sms, message, input));
printf("%s\n", input);
}
else if (choice == 3) {
printf("Program Ended.\n");
}
else {
printf("Invalid Choice.\n");
}
}
getchar();
}
输出如下:
Please choose :
1: SMS to Message.
2: Message to SMS.
3:Exit
1
Please give SMS :OMG
Converting SMS to Message :OH MY GOD
Please choose :
1: SMS to Message.
2: Message to SMS.
3:Exit
1
Please give SMS :WYM
Converting SMS to Message :WONHAT YOU MEAN
Please choose :
1: SMS to Message.
2: Message to SMS.
3:Exit
1
Please give SMS :LOL
Converting SMS to Message :LOSTAUGH OUT LOSTOUD
Please choose :
1: SMS to Message.
2: Message to SMS.
3:Exit
1条答案
按热度按时间s6fujrry1#
几个问题...
fflush(stdin)
是UB [如顶部评论中所述]。sms
和message
是“并行”数组。也就是说,它们的索引方式相同。可以通过使用单个struct
数组来简化它们。replaceWord
为每个单词/短语替换复制 * 整个 * 字符串。1.直接调用
replaceWord
会在每次迭代时泄漏内存。main
中的strcpy
代码泄漏由getMessage
和getSMS
分配和返回的内存。1.使用
strstr
必须大量重新扫描原始字符串。1.更容易/更好的方法是复制原始字符串,然后逐个字符、逐个短语地处理。不需要用临时副本来
malloc
结果字符串。而且,不需要replaceWord
[及其复杂性]。1.大写/小写可以由
strcase*
函数处理(例如,用strcasecmp
代替strcmp
)。下面是修改后的代码:
在上面的代码中,我使用了
cpp
条件语句来表示旧代码和新代码:注意:通过
unifdef -k
运行文件可以清除此问题下面是我用来测试的示例输入(根据您的示例):
下面是程序输出: