程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了试图制作一个打印首字母缩略词的程序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决试图制作一个打印首字母缩略词的程序?

开发过程中遇到试图制作一个打印首字母缩略词的程序的问题如何解决?下面主要结合日常开发的经验,给出你关于试图制作一个打印首字母缩略词的程序的解决方法建议,希望对你解决试图制作一个打印首字母缩略词的程序有所启发或帮助;

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

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;

} here is an image of what it output.

它返回没有意义的值,如“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]

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

解决方法

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

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

我并不是说我的函数是最好的方法或唯一的方法,某些事情我没有解决。例如,我不检查结果数组是否会溢出。但是,为了您的目的,我只是在 main 中使 result 足够大,以便您提供的示例短语不会发生这种情况。但是,根据您提供的内容,它有效,我希望它有助于解决问题。

#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

大佬总结

以上是大佬教程为你收集整理的试图制作一个打印首字母缩略词的程序全部内容,希望文章能够帮你解决试图制作一个打印首字母缩略词的程序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: