C语言项目:开发一个回文诗生成器

   2024-10-20 8010
核心提示:#include stdio.h#include string.h// Function to check if a string is palindromeint isPalindrome(char *str) {int len = st

#include <stdio.h>#include <string.h>

// Function to check if a string is palindromeint isPalindrome(char *str) {int len = strlen(str);for (int i = 0; i < len / 2; i++) {if (str[i] != str[len - i - 1]) {return 0;}}return 1;}

// Function to generate palindrome poem using input linevoid generatePalindromePoem(char *line) {// Split the line into wordschar *word = strtok(line, " “);while (word != NULL) {// Reverse the word and print itint len = strlen(word);for (int i = len - 1; i >= 0; i–) {printf(”%c", word[i]);}printf(" “);word = strtok(NULL, " “);}printf(”\n”);}

int main() {char line[100];

// Get input line from userprintf("Enter a line to generate palindrome poem: ");fgets(line, sizeof(line), stdin);// Remove newline character from inputline[strcspn(line, "\n")] = 0;// Check if input line is palindromeif (isPalindrome(line)) {    // Generate palindrome poem    generatePalindromePoem(line);} else {    printf("Input line is not a palindrome.\n");}return 0;

}

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号