字符串:PigLatin程序问题。请帮忙。 [英] Strings: PigLatin Program Problems. Help pls.

查看:107
本文介绍了字符串:PigLatin程序问题。请帮忙。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<iostream>
#include<string>
#include<fstream>
#include<conio.h>
using namespace std;


 
int main(){
string sSentence;
ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,sSentence) )
    {
      cout << sSentence << '\n';
    }
    myfile.close();
  }
	else cout << "Unable to open file"; 

int iLen = sSentence.length();
int iBlank = sSentence.find(" ");

        int o=0;       
		string sSen = sSentence.substr(o,iBlank);          
		         
	for(;o<iLen;o++){
		
			if( isalpha(sSentence[o])){ 
			
				int iLength = sSen.length();
				int iVowel = sSen.find_first_of("aeiouAEIOU");
				string adSent = sSen.substr(o,iVowel);
				string addSent = sSen.substr(iVowel,iBlank);
				string pig = addSent+adSent;
				string latin = pig.insert(iBlank,"ay");
				cout << latin;
				
				}
		
			else{
				cout<< sSentence[o];
				}
				
			
				
	} 
	
	
return 0;
}






此代码中的
。我想把一个单词翻译成Piglatin。
$。b $文件中的
消息是:

我爱你



该程序打算打印

iay ovelay ouyay



所以它找到了单词中的第一个元音然后元音前的所有字母都将转移到单词的末尾,然后添加字母ay。





在我的程序中循环只适用于它可以找到的第一个单词。



我的问题是我需要将索引移动到单词的最后一个字母。这样当我增加字符串数组的索引时。我会得到空间并打印出来。如何将o指定给单词的最后一个字母的位置?



如果我输入错误




in this code. im suppose to translate a word to Piglatin.
inside the .txt file the message is:
"i love you"

the program is suppose to print
"iay ovelay ouyay"

so it finds the first vowel in the word then all the letters before the vowel will be transfered to the end of the word then add the letters "ay".


in my program the loop there works only for the first word that it can find.

my problem is i need to move my index to the last letter of the word. so that when I increment the index of the string array. i would get the space and print it. how can I assign o to the location of the last letter of the word?

there is an error if i put

o = iBlank-1; 


在if条件结束时
。我需要跳到下一个单词并找到句子中的下一个空格。



请帮忙谢谢:))


at the end of the if condition. i need to jump to the next word and find the next blank space in the sentence.

pls help thank you :))

推荐答案

最好创建一个接受字符串的子例程,将其转换为letlatin形式,然后返回转换后的字符串。您的 main 函数只需要将输入字符串拆分为标记,然后依次将每个标记传递给转换器。以这种方式,转换器不需要关心每个单词的大小,或者它在整个句子中的位置。与往常一样,关键是尝试将问题分解为易于管理的小组件,而不是尝试在一个串行过程中完成所有操作。
It would be better to create a subroutine that accepts a string, converts it to its piglatin form, and return the converted string. Your main function then only needs to split the input string into tokens, and pass each token to the converter in turn. In this way the converter does not need to be concerned about the size of each word, or where it fits in the overall sentence. The key, as always, is to try to break your problem down into small easily managed components, rather than trying to do everything in one serial process.


每个单词都有三个重要的索引,即

  • 起始字母,我们将其命名为 start
  • 元音的索引,让它命名为元音
  • 空白的索引(或字符串的传递结尾),我们称之为结束
You have three important indices for each word, namely
  • The starting letter, let's name it start.
  • The index of the vowel, let's name it vowel
  • The index of the blank (or of the pass-the-end of the string), let's call it end
void piglatin(const string & sSentence)
{
  size_t start, vowel, end;
  start = 0;
  do
  {
    end = sSentence.find(' ', start);
    vowel = sSentence.find_first_of("aeiouAEIOU", start);
    cout << sSentence.substr(vowel, end-vowel) << sSentence.substr(start, vowel-start) << "ay ";
    start = end + 1;
  } while (end != string::npos);
  cout << endl;
}


这篇关于字符串:PigLatin程序问题。请帮忙。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆