⼭⽺拉丁⽂:https://leetcode-cn.com/problems/goat-latin/
总体思路:先将字符串通过空格分割并存入字符数组中,再将字符数组中的各字符串依据题目要求进行变换最后再拼接到一起返回
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
Solution(){}
string toGoatLatin(string S) {
string s[50]; //字符数组
char c = ' ';//空格
int count = 0;//字符数组的下角标计数器
int row = 0;//单个字符的长度
cout << S.length() << endl;
for (int i = 0; i <= S.length(); i++) {
if (S[i]== ' '||i==S.length()) {
s[count] = S.substr(i - row, row);
count++;
row = 0;
}
else {
row++;
}
}
S = ""; //将字符串置空
for (int i = 0; i < count; i++) {
if (s[i][0] != 'a' && s[i][0] != 'e' && s[i][0] != 'i' && s[i][0] != 'o' && s[i][0] != 'u' &&
s[i][0] != 'A' && s[i][0] != 'E' && s[i][0] != 'I' && s[i][0] != 'O' && s[i][0] != 'U') {
s[i] = s[i].substr(1, s[i].length() - 1) + s[i][0];//对字符串首字母进行移位
}
s[i] =s[i]+"ma";
for (int j = 0; j <= i; j++) {
s[i]+="a";
}
S += s[i];//拼接字符串
if (i != (count - 1)) {
S += " ";//添加空格
}
}
return S;
}
};