• 文章
  • 如何將文字分割成兩個或多個字元
釋出
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