首页 > 解决方案 > 试图制作一个打印出首字母缩写词的程序

问题描述

试图编写一个程序,但它给了我一个奇怪的错误,我似乎无法弄清楚它出了什么问题。对于分配,我们逐步使用称为代码的程序。我不确定这是程序问题还是代码错误。这是我的代码:

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("%s\n", p);
return p;

是它输出的图像。

它返回没有意义的值,例如“P????”、“`?@u?”、“0>???”。并且 acroyms 所需的输出被列为意外输出。抱歉,如果这是转发,但我找不到任何相关问题。我确实收到了一些关于指针的错误,但代码仍然适用于在线编译器。

编辑:这些是我从编译器得到的警告,但它仍然运行(在编译器上)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]

编辑:对于这个家庭作业,我们只能编写一个函数。我不知道后端程序的其余部分是什么样的。该程序称为 codestepbystep 这是我所看到的。https://imgur.com/ORjk1xX

标签: cstringpointers

解决方案


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

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

我并不是说我的功能是最好的方法或唯一的方法,以及某些我没有解决的问题。例如,我不检查结果数组是否溢出。但是,出于您的目的,我只是让结果在 main 中足够大,这样您提供的示例短语就不会发生这种情况。但是,根据您提供的内容,它可以工作,我希望它有助于解决问题。

#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: %s\n", phrases[i]);
        printf("Acronym: %s\n\n", 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] = '\0';
       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] = '\0';

    while (indexWordCopy < wordLength) {
        if (isalpha(wordCopy[indexWordCopy]) && !isalpha(wordCopy[indexWordCopy - 1])) {
            word[indexResult++] = toupper(wordCopy[indexWordCopy]);
            word[indexResult] = '\0';
        }
        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

推荐阅读