• 文章
  • 如何将文本分割成两个或多个字符
发布
2012年4月25日 (上次更新: 2012年4月25日)

如何将文本分割成两个或多个字符

评分: 3.2/5 (48 票)
*****
这段代码展示了如何将用户输入的文本分割成两个或多个字符串。如果您正在制作应用程序或游戏,并且希望用户输入类似“装备剑”之类的内容,这将非常有用。此应用程序将单词分割成两个单词,“装备”和“剑”,您可以检查动作“装备”并执行某些操作,例如在本例中它将装备一把剑。

分割两个字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

#define NUM 20

inline void splitString(char c[])
{
        //These are used to hold the temporary words like "equip" or "sword"
	char tempchar1[NUM] = " ";
	char tempchar2[NUM] = " ";

        //This checks through the whole text typed until it reaches a space
	for(int i = 0; i < NUM;i++)
	{
                //Assigns the first word to the tempchar1
		tempchar1[i] = c[i];

                //Checks if it has reached a space
		if(c[i + 1] == ' ')
		{
                        //Used to assign the second word to tempchar2
			for(int r = i + 2;r < NUM;r++)
			{
				tempchar2[r - (i + 2)] = c[r];
			}
			i = NUM;
		}
	}
        //Prints the two characters
	cout << "tempchar1 = \"" << tempchar1 << "\"" << endl <<
		    "tempchar2 = \"" << tempchar2 << "\"" << endl;
}

int main()
{
	char Char[NUM];

	cin.getline(Char, NUM);

	splitString(Char);

	system("pause");
	return 0;
}


如果您想拥有多个项目,您可以这样做

分割三个字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;

#define NUM 20

inline void splitString(char c[])
{
	char tempchar1[NUM] = " ";
	char tempchar2[NUM] = " ";
	char tempchar3[NUM] = " ";

	for(int i = 0; i < NUM;i++)
	{
		tempchar1[i] = c[i];

		if(c[i + 1] == ' ')
		{
			for(int r = i + 2;r < NUM;r++)
			{
				tempchar2[r - (i + 2)] = c[r];

				if(c[r + 1] == ' ')
				{
					for(int q = r + 2; q < NUM;q++)
					{
						tempchar3[q - (r + 2)] = c[q];
					}
					r = NUM;
				}
			}
			i = NUM;
		}
	}
	cout << "tempchar1 = \"" << tempchar1 << "\"" << endl <<
		    "tempchar2 = \"" << tempchar2 << "\"" << endl <<
			"tempchar3 = \"" << tempchar3 << "\"" << endl;
}

int main()
{
	char Char[NUM];

	cin.getline(Char, NUM);

	splitString(Char);

	system("pause");
	return 0;
}


输出结果可能如下 -

在创建基于文字的游戏,并且您希望他们输入咒语然后输入目标或其他类似的内容时,这非常有用。 有了它,就有无限的可能性。

这是一个如何使用此独特功能的示例程序 -

示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>

using namespace std;
 
#define CHAR 30

//Prototypes
void getUserI();
int main();
void catchErrors(char error[CHAR]);

//Convention - c infront of a name = current
int maxhp = 100, pHp = maxhp;
int maxmana = 100, cmana = maxmana;

int playerChoice, numWeapsInInventory = 3;
string tagWeap = "";

int lvl = 12, strength = 1, intelligence = 3, dexterity = 8, agility = 4, maxWeapons = 3;
string cweapon = "Fist", playerJob = "Farmer", playerName = "Demon Slayer";

bool exitGame = false;

void showStat()
{
	system("cls");
	cout << "\tName   : " << playerName << "\n\nWeapon : " << cweapon << "\n\n" << "\tJob    : " << playerJob << "\n";
    cout << "\tLvl    : " << lvl << "\n\tHealth : " << pHp << "/" << maxhp << "\n";
    cout << "\tMana   : " << cmana << "/" << maxmana << "\n\n";
    cout << "Strength     : " << strength << "\n" << "Intelligence : " << intelligence << "\n";
    cout << "Dexterity    : " << dexterity << "\n" << "Agility      : " << agility << "\n\n";
}

void catchErrors(int error)
{
	switch(error)
	{
	case 1:
		cout << "You did not type an action, please try again" << endl;
		break;
	default:
		cout << "ERROR in catchErrors. You should not see this!!" << endl;
		break;
	}

	getUserI();
}

void decipherChar(char c[CHAR])
{
	//Temporary holding characters to hold the action and another variable needed to split
	char tempchar[CHAR] = " ";
	char tempchar2[CHAR] = " ";

	//Checks the char c letter by letter
	for(int i = 0; i < CHAR; i++)
	{
		//Assigns the letters one by one to the tempchar
		tempchar[i] = tolower(c[i]);

		//Checks if it has reached a space or the null character - used for just actions etc exit
		if(c[i + 1] == ' ' || c[i + 1] == '\0')
		{
			//	equip
			if(tempchar[0] == 'e' && tempchar[1] == 'q' && tempchar[2] == 'u' && tempchar[3] == 'i' && tempchar[4] == 'p')
			{
				//Goes through the second part of the string
				for(int w = 6;w < CHAR; w++)
				{
					//Makes sure that it wont add any weird characters for the other 20 or so elements of the array
					if(c[w] != '\0')
					{
						//Assigns the first array in tempchar2 to the weapon
						tempchar2[w - 6] = tolower(c[w]);
					}
					else break;
				}

				cout << "You eqipped " << tempchar2 << endl;
				//Assigns the weapon
				cweapon = tempchar2;
			}

			//	stat
			else if(tempchar[0] == 's' && tempchar[1] == 't' && tempchar[2] == 'a' && tempchar[3] == 't')
			{
				showStat();
			}

			//	exit
			else if(tempchar[0] == 'e' && tempchar[1] == 'x' && tempchar[2] == 'i' && tempchar[3] == 't')
			{
				exitGame = true;
				main();
			}

			else
			{
				catchErrors(1);
			}

			//sets i to char so the for loop ends
			i = CHAR;
		}
	}

	//Goes back to asking the user another choice
	getUserI();
}

void getUserI()
{
	char choice[CHAR];

	//Gets the whole line that the user has typed in including spaces and everything
	cin.getline(choice, CHAR);

	decipherChar(choice);
}

int main()
{
	if( exitGame != true)
	{
		getUserI();
	}

	system("pause");
	return 0;
}


顺便说一下,退出功能无法正常工作,并且某些变量可能未使用,但这是一个关于如何将此功能集成到您的程序中的示例。 可能有更好的方法,我可能会在其他时间发布,也许使用类,但这种方法目前来说非常好。 祝你玩得开心。

Sean Genge