C ++程序,它在字符串中交换两个字符

时间:2015-03-10 07:37:02

标签: c++ arrays string swap gets

我是C ++的初学者,我想编写一个简单的程序,用一个字符串交换两个字符。

例如;我们输入这个字符串:“EXAMPLE”,我们给这两个字符交换:'E'和amp; 'A'和输出应该像“AXEMPLA”。

我在编写下面的代码时考虑的算法;非常简单,我希望你能通过参考代码得到它,但最后我感到困惑! (我在这个网站上搜索并发现了类似的问题,但它们要么是复杂的语法,要么是其他语言的复杂问题)。任何建议和帮助表示赞赏。

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <cctype> // is this necessary?! I doubt between <stodio.h> and <cctype>
// By the way! when do we use double quotations instead of <> ?

using namespace std;

char array1[30], char1, char2;
int i, j, char1count = 0, char2count = 0, locofchar1[15], locofchar2[15], n1 = 0, n2 = 0;

int main()
{
    cout << "Enter your string: " << endl;
    gets(array1);

    cout << "\nEnter the 2 characters you want to swap." << endl
         << "Character #1: ";
    cin  >> char1;
    cout << "\nCharacter #2: ";
    cin  >> char2;

      for (i = 0; array1[i]; i++) // A "for" loop for counting the number of repetitions of char1
// and saving the locations of char1 in a new array called "locofchar1"
           if (array1[i] == char1){
              char1count++;
              for (j = n1; j <= char1count; j++)
                   locofchar1[j] = i;
                   n1++;
     }

      for (i = 0; array1[i]; i++) // Another "for" loop for counting the number of repetitions of char2
// and saving the locations of char1 in a new array called "locofchar2"
           if (array1[i] == char2)
              char2count++;
              for (j = n2; j <= char2count; j++)
                  locofchar2[j] = i;
                  n2++;

/* 

I'm already stuck at here! and I think I have some problems in the above code... We assume that the program determined the number of repetitions and their element address/location in the char1count and char2count arrays, and we want to use this informations to swap them correctly. 

*/

getch();
return 0;
}

5 个答案:

答案 0 :(得分:3)

您忘了在多个位置放置括号。

  for (i = 0; array1[i]; i++) // Another "for" loop for counting the number of repetitions of char2
// and saving the locations of char1 in a new array called "locofchar2"
       if (array1[i] == char2)
          char2count++;
          for (j = n2; j <= char2count; j++)
              locofchar2[j] = i;
              n2++;

如果没有花括号,在C / C ++中,只有if之后的第一个程序处于该条件下。其余的无条件执行。因此,每次执行上面代码中的for循环。 所以添加大括号:

       if (array1[i] == char2) {
          char2count++;
          for (j = n2; j <= char2count; j++) {
              locofchar2[j] = i;
              n2++;
          }
       }

最好为每个条件和每个循环添加大括号,这样就不会忘记哪些行属于这些条件/循环。

答案 1 :(得分:0)

这可能适合你:

for (i = 0; array1[i]; i++)
{
    if (array1[i] == char1)
    {
        array1[i] = char2;
    }
    else if (array1[i] == char2)
    {
        array1[i] = char1;
    }
 }

答案 2 :(得分:0)

您的代码太复杂,任何复杂的代码都包含错误。

尝试以下方法编写循环

#include <iostream>

int main()
{
    char s[] = "EXAMPLE";
    char c1 = 'A', c2 = 'E';

    std::cout << s << std::endl;

    for ( char *p = s; *p; ++p )
    {
        if ( *p == c1 ) *p = c2;
        else if ( *p == c2 ) *p = c1;
    }

    std::cout << s << std::endl;
}

程序输出

EXAMPLE
AXEMPLA

编程中有一个名为KISS的原则 - 保持简单,愚蠢。:) 更简单的代码更容易理解它的作用。

考虑到您应该使用C ++输入功能。如果不使用不安全的C函数gets,你会使用标准C ++函数getline

会更好

答案 3 :(得分:0)

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "";
    char firstChar, secondChar;

    cout << "Enter a string: ";
    getline(cin, str);

    cout << "\nEnter two characters to swap\n";

    cout << "First character: ";
    cin >> firstChar;
    cout << "Second character: ";
    cin >> secondChar;

    for (unsigned char counter = 0; counter < str.size(); counter++)
    {
        if (str[counter] == firstChar)
        {
            str[counter] = secondChar;  // Swap every instance of first
                                        // character with second character.
        }
        else if (str[counter] == secondChar)
        {
            str[counter] = firstChar;  // Swap every instance of second
                                       // character with first character.
        }
    }

    cout << "Result: " << str << endl;

    return 0;
}

您不需要stdioconiocctype。对于这个C ++程序来说,iostream和string就足够了。您通过添加stdioconio来混合使用C和C ++。

关于这个:

// By the way! when do we use double quotations instead of <> ?

访问此link获取答案。

答案 4 :(得分:0)

这看起来有用吗,祝你有愉快的一天。

#include <string>
#include <algorithm>

std::string s = "03/02";
std::swap(s[1], s[4]);