2014-12-19 47 views
0
/* Write a program that would mix-and-merge two given strings (s1 and s2) into string s3 as follows: 
first character of s1, first character of s2, second character of s1, second character of s2, etc. */ 

但是我的代码的问题是:如果我键入S1 "John",然后S2 "Stevens" 结果将是= JSothenv e n s.如何停止检查如果一个字符串空指数

如何解决其中一个字符串结束后留下的空格?

我认为我会修复它的方式是我会检查与ifs我在下面的for循环要查看索引是否为null或'\ 0'但不起作用,因为字符串保存随机值在字符串结束后。

#include <iostream> 
#include <string> 

using namespace std; 

int main() 
{ 
    string s1, s2; 
    string s3; 
    int i; // For index 
    int j = 0; // For second index and loop checking 

    cout << "Type first string: "; 
    getline(cin, s1); 
    cout << "Type second string: "; 
    getline(cin, s2); 

    s3.resize(s1.size() + s2.size() + 100); // The + 100 is used so we have space for all the characters. The + 100 is not needed if i fix my problem. 

    for(i = 0; j <= s1.size(); i += 2) 
    { 
     if(s1[j] == null) // With what do i check it? 
     { 
      break; 
     } 
     else 
     { 
      s3[i] = s1[j]; 
      ++j; 
     } 
    } 

    j = 0; 

    for(i = 1; j <= s2.size(); i += 2) 
    { 
     if(s2[j] == null) 
     { 
      break; 
     } 
     else 
     { 
     s3[i] = s2[j]; 
     } 
     ++j; 
    } 

    for(i = 0; i <= s3.size(); ++i) 
    { 
     cout << s3[i]; 
    } 

    return 0; 
+2

你的第一段很混乱。请重新阅读并尝试考虑如何使其更清晰。提供一套完整的示例输入和输出将有所帮助。 – 2014-12-19 06:37:36

+0

也许你的意思是说:“给定两个输入字符串,我想通过交织输入字符串的字符来产生第三个字符串,例如,给定”DOG“和”章鱼“,我想产生.. 。“ – 2014-12-19 06:40:15

+0

好的,谢谢我会这么做:)现在编辑更好吗? – Johnson 2014-12-19 06:40:44

回答

1

试试这个:

#include <iostream> 
#include <string> 

using namespace std; 

int main(int argc, char const *argv[]) 
{ 
    string s1, s2, s3; 

    int i,j,k; 
    cout << "Type first string: "; 
    getline(cin, s1); 
    cout << "Type second string: "; 
    getline(cin, s2); 
    s3.resize(s1.size()+s2.size()); 

    for(i = 0, j = 0, k = 0; j < s1.size() && k < s2.size(); i++) { 
     if(i & 1) { 
      s3[i] = s2[k++]; 
     } else { 
      s3[i] = s1[j++]; 
     } 
    } 
    if(j == s1.size()) { 
     while(k < s2.size()) { 
      s3[i++] = s2[k++]; 
     } 
    } else { 
     while(j < s1.size()) { 
      s3[i++] = s1[j++]; 
     } 
    } 

    cout << s3 << endl; 

    return 0; 
} 
+0

这个作品我的朋友,但你能解释一下它是如何工作的,这会导致我很困惑 – Johnson 2014-12-19 07:20:39

+0

for循环基本上交错s1'和's2'直到其中一个结束。然后if语句简单地将剩余部分添加到's3'。 – justmscs 2014-12-19 07:25:21

+0

你能解释一下这个部分吗? if(i&1)' – Johnson 2014-12-19 07:29:33

1

你可以用这个getline后替换代码:

string::const_iterator i1 = s1.begin(), i2 = s2.begin(); 
    bool useFirst = true; 

    while (i1 != s1.end() || i2 != s2.end()) 
    { 
    if (useFirst && i1 != s1.end()) 
    { 
     s3.push_back(*i1++); 
    } 
    else if (i2 != s2.end()) 
    { 
     s3.push_back(*i2++); 
    } 

    useFirst = !useFirst; // switch over for next iteration 
    } 

    cout << s3 << endl; 

这是一个有点简单,基本上迭代交替在两个字符串,直到它们”重新都疲惫不堪。

+0

谢谢你现在的作品,但结果有点奇怪。喜欢而不是有结果'JSothenv e n s'我有结果:JS还是像开始有许多空间一样。编辑堆栈溢出不会让我显示它,但在第二个结果中,在显示正确的输出'JSothenv e n s'之前,请求中有很多空格。 – Johnson 2014-12-19 07:15:28

1

这是相当简单写一个通用interleave的算法,在两个迭代范围一次:

template<typename InputIterator1, typename InputIterator2, typename OutputIterator> 
OutputIterator interleave(InputIterator1 first1, InputIterator1 end1, 
          InputIterator2 first2, InputIterator2 end2, 
          OutputIterator out) 
{ 
    while(first1 != end1 && first2 != end2) 
    { 
    out = *first1; 
    out = *first2; 
    ++first1; 
    ++first2; 
    } 
    // copy remaining elements from both ranges 
    while(first1 != end1) 
    { 
    out = *first1; 
    ++first1; 
    } 
    while(first2 != end2) 
    { 
    out = *first2; 
    ++first2; 
    } 
    return out; 
} 

Live demo here。用法是一样std::set_merge

interleave(begin(s1), end(s1), 
      begin(s2), end(s2), 
      std::back_inserter(result)); 

这两个范围遍历直到有用完,然后复制在这两个范围内剩余的元素。在两个容器之间迭代一次,并且有一个双重结束检查,如果不假设一个范围比另一个更大,我就无法摆脱它。现在可以处理两个不同的容器

+0

符合条件的'std :: begin'和'std :: end'对我来说有点像反模式。我会说使用自由函数开始/结束应该使用ADL像范围为。另外,我会一直写'while'循环(循环都是结构相同的)。最后,你会想要允许混合的输入范围。 – sehe 2014-12-19 08:47:59

+0

@sehe Done。从来没有想过这样(ADL)。谢谢你的提示。 – rubenvb 2014-12-19 08:50:22

+0

这里是我的建议版本http://coliru.stacked-crooked.com/a/ce939199348423ee(我知道它可能会做更多的比较,但我可以说让编译器在分析后优化/担心优化)。看看它如何返回输出迭代器(约定),它可以采用不同的迭代器类型 – sehe 2014-12-19 08:56:44

相关问题