C语言 试图编写一个程序,打印出一个首字母缩略词



试图写一个程序,但它给了我一个奇怪的错误,我似乎无法找出它错了什么。对于赋值,我们使用一个叫做code的程序一步一步地进行。我不确定这是一个程序问题,或者如果代码是错误的。下面是我的代码:

char* acronym(char* word){
char result[50];
strcpy(result, "");
int index = 0;
for(int i = 0; i < strlen(word); i++){
if(isalpha(word[i]) && !isalpha(word[i-1])){
char upper = toupper(word[i]);
char * add = &upper;
strncat(result, add, 1);
}
}
char *p = &result;
printf("%sn", p);
return p;

}下面是它输出的图像:

返回没有意义的值,如" p ????"," ' ?@u?", "0>?? "。所需的输出(首字母缩写)被列为意外输出。抱歉,如果这是一个转发,但我找不到任何相关的问题。我确实得到了一些关于指针的错误,但代码仍然可以在在线编译器上工作。

编辑:这些是我从编译器得到的警告但它仍然运行(在编译器上)main.c:17:12:警告:函数' isalpha '的隐式声明[-Wimplicit-function-declaration]main.c:18:26:警告:函数' toupper '的隐式声明[-Wimplicit-function-declaration]main.c:23:15:警告:从不兼容的指针类型初始化[-Wincompatible-pointer-types]main.c:28:1:警告:返回类型默认为' int ' [-Wimplicit-int]

编辑:对于这个家庭作业,我们只能写一个函数。我不知道程序后端的其余部分是什么样子。这个程序叫做codestep,这是我看到的。https://i.stack.imgur.com/6Lddh.jpg

人们已经在评论中提出了你的基本问题。如果你仍然卡住了,这里有一个有效的程序来帮助你解决你的问题。我做了两个根本性的改变。首先,正如其他人所讨论的,您不能返回一个局部变量的数组。我在main中声明了它并把它作为参数传递。其次,检查前一个字符是否不是字母,有一个边缘情况,即您在第一个字符上,即索引0。在这种情况下,您不能检查回一个字符,因为这会将您带到不存在的索引-1。因此,我首先有一个特殊的while循环来处理首字母缩写的第一个字母。

在第一个字母之后,类似的东西可以正常工作。

我并不是说我的函数是最好的或唯一的方法,有些事情我没有提到。例如,我不检查结果数组是否溢出。然而,为了你的目的,我只是使结果在主要的足够大,这样就不会发生你提供的例子短语。但是,根据您提供的内容,它可以工作,我希望它有助于澄清问题。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char* acronym(char *word);
int main(int argc, char **argv) {
char phrases[8][100] = { " automatic teller machine", "personal identification number",
"computer science", "merry-go-round", "All my Children", "Trouble Assets Relief Program",
"--quite-- confusing - punctuation-", "loner " };
unsigned phrasesLength = sizeof(phrases) / sizeof(phrases[0]);
for (unsigned i = 0; i < phrasesLength; i++) {
printf("Phrase: %sn", phrases[i]);
printf("Acronym: %snn", acronym(phrases[i]));
}
return 0;
}
char* acronym(char* word) {
char wordCopy[500]; // wil hold a copy of word. Make large enough to avoid overflow
unsigned wordLength = strlen(word);
/* To use a function to copy word into wordCopy, you could do
wordCopy[i] = '';
strcpy(wordCopy, word);
*/
// Here's how to do the copy manually
for (unsigned i = 0; i < wordLength + 1; i++)
wordCopy[i] = word[i];

// wordCopy is now a copy of word
unsigned indexWordCopy = 0; // start at first character of phrase
unsigned indexResult = 0;   // start at first character of word
// this will override the characters that were
// there previously
// iterate through word copy character by character. Store the acronym in word
// which right now contains the original phrase
while (!isalpha(wordCopy[indexWordCopy]))
indexWordCopy++;
word[indexResult++] = toupper(wordCopy[indexWordCopy++]);
word[indexResult] = '';
while (indexWordCopy < wordLength) {
if (isalpha(wordCopy[indexWordCopy]) && !isalpha(wordCopy[indexWordCopy - 1])) {
word[indexResult++] = toupper(wordCopy[indexWordCopy]);
word[indexResult] = '';
}
indexWordCopy++;
}
return word;
}

输出
Phrase:  automatic teller machine
Acronym: ATM
Phrase: personal identification number
Acronym: PIN
Phrase: computer science
Acronym: CS
Phrase: merry-go-round
Acronym: MGR
Phrase: All my Children
Acronym: AMC
Phrase: Troubled Assets Relief Program
Acronym: TARP
Phrase: --quite-- confusing - punctuation-
Acronym: QCP
Phrase:  loner
Acronym: L

最新更新