如何拆分等式的字符串
how to split a string for equation
我是新来的,如有错误请见谅。
我需要一个将字符串拆分为字符的程序。我的意思是如果用户写 "Hello";
那个程序应该使它成为 "a[]={'H','e','l','l','o'}"
或类似的东西。用 C++ 真的可以吗?我要用它来计算方程式。如果你有更好的想法(没有ax^2+bx+c
),请告诉我。谢谢你的帮助。祝你有个愉快的一天。
从字符串中提取字符并将它们存储到字符数组中
简单方法(使用字符串索引):
#include<iostream>
using namespace std;
// Using the namespace std to use string.
int main(int argc, char** argv) {
string input;
cout << "Enter a string: ";
cin>>input; // To read the input from the user.
int strLength = input.length(); // To compute the length of the string.
char * charArray = new char[strLength]; //Dynamic memory allocation
/**
* The following for loop converts the string into an array.
*/
for (int i=0; i<strLength; i++) {
charArray[i] = input[i];
}
cout << endl << "The elements of the character array are as follows: {";
for (int i = 0; i < strLength; i++) {
if (i != strLength - 1) {
cout << "\'" << charArray[i] << "\', ";
} else {
cout << "\'" << charArray[i] << "\'}";
}
}
delete [] charArray;
return 0;
}
示例输出:
Enter a string: Hello
The elements of the character array are as follows: {'H', 'e', 'l', 'l', 'o'}
RUN SUCCESSFUL (total time: 3s)
要处理更复杂的输入,或者如果您对更复杂的结构感兴趣,以下程序应该对您有所帮助:
#include<iostream>
using namespace std;
// Using the namespace std to use string.
int main(int argc, char** argv) {
string input;
cout << "Enter a string: ";
cin>>input; // To read the input from the user.
int strLength = input.length(); // To compute the length of the string.
char * charArray = new char[strLength]; //Dynamic memory allocation
int loopVar = 0;
/**
* The following for loop converts the string into an array.
*/
for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
charArray[loopVar] = *iterativeVar;
++loopVar;
}
cout << endl << "The elements of the character array are as follows: {";
for (int i = 0; i < strLength; i++) {
if (i != strLength - 1) {
cout << "\'" << charArray[i] << "\', ";
} else {
cout << "\'" << charArray[i] << "\'}";
}
}
delete [] charArray;
return 0;
}
示例输出:
Enter a string: Hello
The elements of the character array are as follows: {'H', 'e', 'l', 'l', 'o'}
RUN SUCCESSFUL (total time: 2s)
记得在for
循环中使用const char *
。否则会报错如下:
newmain.cpp: In function 'int main(int, char**)':
newmain.cpp:23:42: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
for (char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
不要忘记使用 delete
释放内存,该内存使用 new
运算符分配给字符数组,如下所示:
delete [] charArray;
否则,您的程序可能会遇到内存泄漏。
从字符串中提取整数字符并将它们存储到整数数组中:
以下程序应该对您有所帮助:
#include<iostream>
using namespace std;
// Using the namespace std to use string.
int main(int argc, char** argv) {
string input;
cout << "Enter a number: ";
cin>>input; // To read the input from the user.
int strLength = input.length(); // To compute the length of the string.
int * intArray = new int[strLength]; //Dynamic memory allocation
int loopVar = 0;
/**
* The following for loop converts the string into an array.
*/
for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
intArray[loopVar] = (*iterativeVar-'0');
++loopVar;
}
cout << endl << "The elements of the integer array are as follows: {";
for (int i = 0; i < strLength; i++) {
if (i != strLength - 1) {
cout << intArray[i] << ", ";
} else {
cout <<intArray[i]<<"}";
}
}
delete [] intArray;
return 0;
}
示例输出:
Enter a number: 123456789
The elements of the integer array are as follows: {1, 2, 3, 4, 5, 6, 7, 8, 9}
RUN SUCCESSFUL (total time: 4s)
我是新来的,如有错误请见谅。
我需要一个将字符串拆分为字符的程序。我的意思是如果用户写 "Hello";
那个程序应该使它成为 "a[]={'H','e','l','l','o'}"
或类似的东西。用 C++ 真的可以吗?我要用它来计算方程式。如果你有更好的想法(没有ax^2+bx+c
),请告诉我。谢谢你的帮助。祝你有个愉快的一天。
从字符串中提取字符并将它们存储到字符数组中
简单方法(使用字符串索引):
#include<iostream>
using namespace std;
// Using the namespace std to use string.
int main(int argc, char** argv) {
string input;
cout << "Enter a string: ";
cin>>input; // To read the input from the user.
int strLength = input.length(); // To compute the length of the string.
char * charArray = new char[strLength]; //Dynamic memory allocation
/**
* The following for loop converts the string into an array.
*/
for (int i=0; i<strLength; i++) {
charArray[i] = input[i];
}
cout << endl << "The elements of the character array are as follows: {";
for (int i = 0; i < strLength; i++) {
if (i != strLength - 1) {
cout << "\'" << charArray[i] << "\', ";
} else {
cout << "\'" << charArray[i] << "\'}";
}
}
delete [] charArray;
return 0;
}
示例输出:
Enter a string: Hello The elements of the character array are as follows: {'H', 'e', 'l', 'l', 'o'} RUN SUCCESSFUL (total time: 3s)
要处理更复杂的输入,或者如果您对更复杂的结构感兴趣,以下程序应该对您有所帮助:
#include<iostream>
using namespace std;
// Using the namespace std to use string.
int main(int argc, char** argv) {
string input;
cout << "Enter a string: ";
cin>>input; // To read the input from the user.
int strLength = input.length(); // To compute the length of the string.
char * charArray = new char[strLength]; //Dynamic memory allocation
int loopVar = 0;
/**
* The following for loop converts the string into an array.
*/
for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
charArray[loopVar] = *iterativeVar;
++loopVar;
}
cout << endl << "The elements of the character array are as follows: {";
for (int i = 0; i < strLength; i++) {
if (i != strLength - 1) {
cout << "\'" << charArray[i] << "\', ";
} else {
cout << "\'" << charArray[i] << "\'}";
}
}
delete [] charArray;
return 0;
}
示例输出:
Enter a string: Hello The elements of the character array are as follows: {'H', 'e', 'l', 'l', 'o'} RUN SUCCESSFUL (total time: 2s)
记得在for
循环中使用const char *
。否则会报错如下:
newmain.cpp: In function 'int main(int, char**)': newmain.cpp:23:42: error: invalid conversion from 'const char*' to 'char*' [-fpermissive] for (char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
不要忘记使用 delete
释放内存,该内存使用 new
运算符分配给字符数组,如下所示:
delete [] charArray;
否则,您的程序可能会遇到内存泄漏。
从字符串中提取整数字符并将它们存储到整数数组中:
以下程序应该对您有所帮助:
#include<iostream>
using namespace std;
// Using the namespace std to use string.
int main(int argc, char** argv) {
string input;
cout << "Enter a number: ";
cin>>input; // To read the input from the user.
int strLength = input.length(); // To compute the length of the string.
int * intArray = new int[strLength]; //Dynamic memory allocation
int loopVar = 0;
/**
* The following for loop converts the string into an array.
*/
for (const char* iterativeVar = input.c_str(); *iterativeVar; iterativeVar++) {
intArray[loopVar] = (*iterativeVar-'0');
++loopVar;
}
cout << endl << "The elements of the integer array are as follows: {";
for (int i = 0; i < strLength; i++) {
if (i != strLength - 1) {
cout << intArray[i] << ", ";
} else {
cout <<intArray[i]<<"}";
}
}
delete [] intArray;
return 0;
}
示例输出:
Enter a number: 123456789 The elements of the integer array are as follows: {1, 2, 3, 4, 5, 6, 7, 8, 9} RUN SUCCESSFUL (total time: 4s)