C语言:仅用stdio.h实现指定单词按规则修改(留前两字符+加横杠+反转剩余)
实现指定单词的规则化修改(仅依赖stdio.h)
需求说明
对句子中的指定单词按以下规则修改:
- 保留单词的前两个字符
- 在这两个字符后添加横杠
- - 反转单词的剩余部分
示例
原句子:“This is the input sentence”,指定单词:“This”
输出:“Th-si is the input sentence”原句子:“This is the input sentence”,指定单词:“input”
输出:“This is the in-tup sentence”原句子:“This is the input sentence”,指定单词:“sentence”
输出:“This is the input se-ecnetn”
已知条件:指定单词长度至少为4,且一定存在于句子中。
现有代码
#include <stdio.h> void put_word(char *word, char *sentence, int index) { // Replace the chars of the sentence with the chars of // the word through the index // Two counter, one for sentence, other for word for (int i = index, c = 0; word[c] != '\0'; i++, c++) { sentence[i] = word[c]; } return; } void reverse_word(char *word) { int size = 0; for (int i = 0; word[i] != '\0'; i++) { size++; } // Size of the word char tmp; for (int i = 0; i < size / 2; i++) // string mirroring { tmp = word[i]; word[i] = word[size -(i + 1)]; // +1 is for ignore \0 word[size - (i + 1)] = tmp; } return; } int find_word(char *word, char *sentence) { // Return index of the start of the word in sentence int counter = 0; for (int i = 0; sentence[i] != '\0'; i++) // Stop at the end of the sentence { // If both sentence[i] and word[counter] == '\0' so this is // should return because of this have the word[counter] != '\0' if (sentence[i] == word[counter] && word[counter] != '\0') { counter++; // Iterate over word } else if (counter != 0) // Two options, or word is founded or not { if (word[counter] == '\0') // Indicates of the found point { return i - counter; // Basic math } counter = 0; // If not founded reset counter word } } return -1; // Not found } int main() { char s[] = "This is the input sentence"; char change_what[] = "input"; int index = find_word(change_what, s); if (index == -1) { printf("Word %s not found!\n", change_what); return 1; } // As these functions work with pointers, the return is not necessary. reverse_word(change_what); put_word(change_what, s, index); printf("\"%s\"", s); return 0; }
修改方案及代码
现有代码的问题是直接反转整个指定单词并替换,不符合需求。需要调整逻辑:先处理单词(保留前两位、加横杠、反转剩余部分),同时因为修改后的单词比原单词长1个字符,需要先给句子腾出空间。
以下是修改后的完整代码:
#include <stdio.h> // 反转字符串的指定区间 [start, end) void reverse_range(char *str, int start, int end) { char tmp; for (int i = start; i < (start + end) / 2; i++) { tmp = str[i]; str[i] = str[start + end - 1 - i]; str[start + end - 1 - i] = tmp; } } int find_word(char *word, char *sentence) { int counter = 0; for (int i = 0; sentence[i] != '\0'; i++) { if (sentence[i] == word[counter] && word[counter] != '\0') { counter++; } else if (counter != 0) { if (word[counter] == '\0') { return i - counter; } counter = 0; } } // 循环结束后检查是否匹配完成 if (word[counter] == '\0') { int len = 0; while(sentence[len]) len++; return len - counter; } return -1; } int main() { char s[] = "This is the input sentence"; char change_what[] = "input"; int index = find_word(change_what, s); if (index == -1) { printf("Word %s not found!\n", change_what); return 1; } // 计算原单词长度 int word_len = 0; while (change_what[word_len] != '\0') { word_len++; } // 计算句子总长度 int sent_len = 0; while (s[sent_len] != '\0') { sent_len++; } // 给句子腾出1个字符的空间(因为要加横杠) // 从单词结束位置开始,把所有字符往后移1位 for (int i = sent_len; i >= index + word_len; i--) { s[i + 1] = s[i]; } s[sent_len + 1] = '\0'; // 更新字符串结束符 // 保留前两个字符 s[index] = change_what[0]; s[index + 1] = change_what[1]; // 添加横杠 s[index + 2] = '-'; // 复制原单词剩余部分到句子中,然后反转 for (int i = 0; i < word_len - 2; i++) { s[index + 3 + i] = change_what[2 + i]; } // 反转剩余部分(从index+3开始,长度是word_len-2) reverse_range(s, index + 3, index + 3 + (word_len - 2)); printf("\"%s\"", s); return 0; }
关键修改点说明
- 新增
reverse_range函数:可以反转字符串的指定区间,避免反转整个单词,只处理从第3位开始的剩余部分。 - 句子空间调整:因为修改后的单词比原单词多1个字符(横杠),需要将单词后面的所有字符向后移动1位,确保有足够空间写入新内容。
- 分步处理单词:先保留前两个字符,添加横杠,再复制原单词剩余部分并反转,最后写入句子对应位置。
内容的提问来源于stack exchange,提问作者basakcglyn
相关产品推荐
相关产品推荐

