在C语言中从句子提取首字母缩略词:现有代码求助
Hey there! Let's fix up your code to extract acronyms from a sentence properly. I noticed a few issues in your current code, so let's break them down and build a working version step by step.
Fixing the Acronym Extraction Code in C
First, let's go over the key issues in your current code:
- Syntax errors in for loops: You used commas
,instead of semicolons;to separate the loop's initialization, condition, and increment components—this will cause the compiler to throw errors. - Incorrect input reading:
scanf("%s", &word[i])won't capture the full sentence since%sstops at whitespace. Plus, you're incorrectly indexing into thewordarray here; you should read the entire line directly intoword. - Incomplete logic: The condition to pick first letters is cut off, and you didn't account for the first character of the sentence or characters that come right after spaces (the start of new words).
Here's a corrected, working version of the code with detailed comments:
#include<stdio.h> #include<string.h> int main() { char sentence[1000]; char acronym[1000]; int i, acronym_index = 0; printf("Please enter a sentence no larger than 1000 characters: "); // Use fgets to read the entire line including spaces fgets(sentence, sizeof(sentence), stdin); // Remove the trailing newline character added by fgets (when user presses enter) size_t sentence_length = strlen(sentence); if (sentence_length > 0 && sentence[sentence_length - 1] == '\n') { sentence[sentence_length - 1] = '\0'; sentence_length--; } // Capture the first character if it's a letter if ((sentence[0] >= 'A' && sentence[0] <= 'Z') || (sentence[0] >= 'a' && sentence[0] <= 'z')) { // Convert to uppercase for a consistent acronym format acronym[acronym_index++] = (sentence[0] >= 'a') ? sentence[0] - 32 : sentence[0]; } // Iterate through the rest of the sentence to find word starts (after spaces) for (i = 1; i < sentence_length; i++) { // Check if current character is a space, and the next character is a letter if (sentence[i] == ' ' && i + 1 < sentence_length) { char next_char = sentence[i+1]; if ((next_char >= 'A' && next_char <= 'Z') || (next_char >= 'a' && next_char <= 'z')) { acronym[acronym_index++] = (next_char >= 'a') ? next_char - 32 : next_char; } } } // Add null terminator to the acronym string (critical for valid C strings) acronym[acronym_index] = '\0'; printf("Generated Acronym: %s\n", acronym); return 0; }
Key improvements explained:
- Using
fgetsinstead ofscanf: This reads the entire line including spaces, which is essential for processing full sentences instead of single words. - Stripping the trailing newline:
fgetsincludes the\ncharacter when the user presses enter, so we remove it to avoid including it in our processing. - Consistent uppercase output: We convert all extracted letters to uppercase so the acronym looks clean (e.g., "hello world" becomes "HW" instead of "hW").
- Proper word-start detection: We capture the first character of the sentence, then check every space to find the start of subsequent words.
- Null terminator for the acronym: Always add
\0to the end of string arrays in C to avoid undefined behavior when printing or manipulating the string.
Let's test this with a couple examples:
Input: "Stack Overflow is Awesome"
Output: "SOIA"
Input: "my favorite programming language"
Output: "MFPL"
内容的提问来源于stack exchange,提问作者user18
相关产品推荐
相关产品推荐

