C++ programming inputs a string from the keyboard, converts the numeric characters in it into numerical values, stores them in an array, and outputs them
Procedure 1:
#include
#include
using namespace std;
int main()
{
char *ch=new char[]; //define a dynamic char array
int *num=new int[]; //define a dynamic int array
cout<<"Please enter a string of characters:"<
cin>>ch; //input string
cout<<"The numbers in this string are: "<
int a=0;
for(int i=0;i
{
if(ch[i]>='0'&&ch[i]<='9')
{
num[a]=(int)ch[i]-48;//save
cout<
a++;
}
}
cout<
return 0;
}
Procedure 2:
#include
using namespace std;
int main()
{
char str[]="l34lab454ii876ui43"; //You can define one by yourself or input one from the keyboard
char *p=str;
int i=0; // Calculate the number of numeric characters
int j=0; //control loop output and assignment
while(*p)
{
if(*p>='0'&&*p<='9')
{
i++;
}
p++;
}
p=str; // just now p ran to the end, point to str again
int *pArray=new int[i]; //Create an integer array with i elements to store digital characters
while(*p)
{
if(*p>='0'&&*p<='9')
{
*(pArray+j)=*p-'0'; // Subtract '0' from the numeric character to get the integer, and assign it to the array just created
j++;
}
p++;
}
// Output
for(j=0;j
{
cout<<*(pArray+j)<<" ";
}
cout<
Reference address:A program that stores the numeric characters in a string separately from other characters
return 0;
}
Previous article:Timer A of msp430 is divided into several independent time intervals
Next article:MSP430 5438 MCU Study Notes
- Popular Resources
- Popular amplifiers
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
Guess you like
- Share: Capsule Robot Antenna Design Information (English)
- The award-winning live broadcast will start at 10:00 this morning: "Dadatong helps you unlock the new generation of ADAS technology"
- Free Pinouts eBook (The Pinouts Book) V0.3
- [ESK32-360 Review] 4. Run the virtual serial port routine
- 【TI recommended course】#Lecture on basic knowledge of electronic circuits#
- Low-pass filter waveform distortion problem
- Questions about PCB antenna
- [RVB2601 Creative Application Development] RVB2601 Hardware Part--1
- #Servo What is a servo?
- Questions about rotary transformers