2016-11-28 131 views
-1

我已经查找了许多如何执行此操作的示例,但似乎没有一个正常工作。 我有一个像这样的字符串将第一个单词移至字符串末尾

Fen' Harel, Solas 
    De Fer, Vivienne 
    Rainier, Thom 
    Blackwall, Gordon 
    Nightingale, Leliana 

与名称的文件有多个名称,但是这仅仅是一个例子 名称的格式为姓氏,名字 我想在订单名姓//与输出它们没有 “”

// before this is the functions getting the data from the string 
//and putting it into arrays, and they all work 

//This function loops the output for each set of names in the file 
void output(string n[], int s){ 

for(int i = 0; i<s;i++){ 
    formatName(n[i]); 
cout<<endl; 
} 
} 
// This function adjusts the format of the names 
void formatName(string names){ 
    string name =""; 
    int size = 0; 
    size = names.find(","); 
    for(int i=0;i<size;i++){ 
    name = moveFirstToLast(names); 
    } 

    cout<<name; 
} 
//This function is what should move the first name to the last name but is not working 
string moveFirstToLast(string name){ 
    name = name + name[0]; 
    name.erase(0,1); 
    return name; 
} 

当程序运行时显示

en Harel, Solas 
De Fer, Vivienne 
ainier, Thom 
lackwall, Gordon 
ightingale, Leliana 

以下所以AP roblem其中的第一个名字不被追加到名称,也母鹿鼻涕似乎循环和由于某些原因,第二个名字被跳过完全

+0

所以'formatName'需要名称** s **?不,它没有。它需要**一个**名称,并且必须重复执行'name = moveFirstToLast(name);'。函数的前两行是无用的(当你重命名参数并执行'size_t size = names.find(“,”);')时,就像你应该拥有的一样。 – LogicStuff

+0

这样做的算法是:1.反转整个数组。 2.找到逗号。 3.从逗号之前的字符串开始到(但不包括)空格的所有内容。 4.反转从(包括)逗号到字符串结尾的所有内容。 5.删除逗号(字符串中的最后一个字符)。完成,并且不需要中间缓冲区。 – WhozCraig

回答

相关问题