⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 piglatin.c

📁 书名:C语言科学与艺术,以前交钱下载的
💻 C
字号:
/* * File: piglatin.c * ---------------- * This program translates a line of text from English * to Pig Latin.  The rules for forming Pig Latin words * are as follows: * * o  If the word begins with a vowel, add "way" to the *    end of the word. * * o  If the word begins with a consonant, extract the set *    of consonants up to the first vowel, move that set *    of consonants to the end of the word, and add "ay". */#include <stdio.h>#include <ctype.h>#include "genlib.h"#include "strlib.h"#include "simpio.h"#include "scanner.h"/* Private function prototypes */static void TranslateLine(string line);static bool IsLegalWord(string token);static string TranslateWord(string word);static int FindFirstVowel(string word);static bool IsVowel(char ch);/* Main program */main(){    string line;    printf("Enter a line: ");    line = GetLine();    TranslateLine(line);}/* * Function: TranslateLine * Usage: TranslateLine(line); * --------------------------- * This function takes a line of text and translates * the words in the line to Pig Latin, displaying the * translation as it goes. */static void TranslateLine(string line){    string token;    InitScanner(line);    while (!AtEndOfLine()) {        token = GetNextToken();        if (IsLegalWord(token)) token = TranslateWord(token);        printf("%s", token);    }    printf("\n");}/* * Function: IsLegalWord * Usage: if (IsLegalWord(token)) ... * ---------------------------------- * IsLegalWord returns TRUE if every character in the argument * token is alphabetic. */static bool IsLegalWord(string token){    int i;    for (i = 0; i < StringLength(token); i++) {        if (!isalpha(IthChar(token, i))) return (FALSE);    }    return (TRUE);}/* * Function: TranslateWord * Usage: word = TranslateWord(word) * --------------------------------- * This function translates a word from English to Pig Latin * and returns the translated word. */static string TranslateWord(string word){    int vp;    string head, tail;    vp = FindFirstVowel(word);    if (vp == -1) {        return (word);    } else if (vp == 0) {        return (Concat(word, "way"));    } else {        head = SubString(word, 0, vp - 1);        tail = SubString(word, vp, StringLength(word) - 1);        return (Concat(tail, Concat(head, "ay")));    }}/* * Function: FindFirstVowel * Usage: k = FindFirstVowel(word); * -------------------------------- * FindFirstVowel returns the index position of the first vowel * in word.  If word does not contain a vowel, FindFirstVowel * returns -1. */static int FindFirstVowel(string word){    int i;    for (i = 0; i < StringLength(word); i++) {        if (IsVowel(IthChar(word, i))) return (i);    }    return (-1);}/* * Function: IsVowel * Usage: if (IsVowel(ch)) . . . * ----------------------------- * IsVowel returns TRUE if ch is a vowel.  This function * recognizes vowels in either upper or lower case. */static bool IsVowel(char ch){    switch (tolower(ch)) {      case 'a': case 'e': case 'i': case 'o': case 'u':        return (TRUE);      default:        return (FALSE);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -