string类的基本使用,编写一个字符串处理程序

这是一个字符串处理程序,按照老外的注释的意思就是将英文单词按一定的规则转换成为某种拉丁文字。

运用到截取字符串,合并字符串,遍历字符串等规则,感觉它的结构非常清晰。

/*
 * This program converts lines from English to Pig Latin
 * This dialect of Pig Latin applies the following rules:
 * 1. If the word contains no vowels, return the original word unchange.
 * 2. If the word begins with a consonant, extract the set of consonants up
 *    to the first vowel, move that set of consontants to the end of the word,
 *    and add "ay".
 * 3. If the word begins with a vowel, add "way" to the end of the word
 */

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

string lineToPigLatin(string line);
string wordToPigLatin(string word);
int findFirstVowel(string word);
bool isVowel(char ch);

int main(int argc, char *argv[])
{
    cout << "This program translates English to Pig Latin." << endl;
    string line;
    cout << "Enter English text: ";
    getline(cin, line);
    string translation = lineToPigLatin(line);
    cout << "Pig Latin output: " << translation << endl;
    return 0;
}

/*
 * Translates each word in the line to Pig Latin, leaving all other characters
 * unchanged. The variable start keeps track of the index position at which
 * the current word begin. As a special case the code sets start to -1 indicate
 * that the beginning of the current word has not yet been encountered.
 */
string lineToPigLatin(string line) {
    string result;
    int start = -1;
    for (int i = 0; i <line.length(); ++i) {
        char ch = line[i];
        if (isalpha(ch)) {
            if (start == -1) start = i;
        } else {
            if(start >= 0) {
                result += wordToPigLatin(line.substr(start, i - start));
                start = -1;
            }
            result += ch;
        }
    }
    if (start >= 0) result += wordToPigLatin(line.substr(start));
    return result;
}

string wordToPigLatin(string word) {
    int vp = findFirstVowel(word);
    if (vp == -1) {
        return word;
    } else if (vp == 0) {
        return word + "way";
    } else {
        string head = word.substr(0, vp);
        string tail = word.substr(vp);
        return tail + head + "ay";
    }
}

int findFirstVowel(string word) {
    for (int i = 0; i < word.length(); ++i) {
        if (isVowel(word[i])) return i;
    }
    return -1;
}

bool isVowel(char ch) {
    switch (ch) {
    case 'A': case 'E': case 'I': case 'O': case 'U':
    case 'a': case 'e': case 'i': case 'o': case 'u':
        return true;
    default:
        return false;
    }
}

运行结果

This program translates English to Pig Latin.
Enter English text: Program to translate English to Pig Latin
Pig Latin output: ogramPray otay anslatetray Englishway otay igPay atinLay
Press <RETURN> to close this window...

stanford的stdlib.h功能都已经写好了,拿来直接用就可以啦。

If you use the >> extraction operator to read string data, the input stops at the first whitespace character. If you want to read an entire line of text from the user, it is usually better to use the getline function from the standard C++ libraries.

review question

1. 一个是字符,一个是字符串。

2. false

3. string

4. free function 不需要对象来调用。

5. false


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值