# High level language programming -- Chapter 6: pointers

Posted by Mackey18 on Fri, 17 Jan 2020 11:32:02 +0100

# Chapter VI indicators

## 1. Count the number of integers

[problem description] input a string, including numeric and non numeric characters, such as: a123x456 17935? 098tab, take the consecutive numbers as an integer, store them in array a in turn, count the total number of integers, and output these numbers.

[input form] string of numeric and non numeric characters

[output form] 1) number of integers 2) output integers respectively
[sample input] a123x456 17935? 098tab583 [note that strings with spaces need to be reserved, please do not use gets, cin, practice using cin.getline(char *str, int maxnum)]

[sample output]

5

123

456

17935

98

583

[sample description] the first output item is the number of integers, and the following are specific integers. Note that you do not need to output prompt type text, such as "integer is", "respectively is". Output the results directly. There is a number to output. There are no consecutive numbers in the test case that exceed the integer range. When a number beginning with 0 is encountered, it should be rounded off.

```#include <iostream>
using namespace std;
int main()
{char str[50],*pstr;
int i,j,k,m,e10,digit,ndigit,a[10],*pa;
cin.getline(str,50);
pstr=&str[0];    //Character pointer pstr points to the first element of array str
pa=&a[0];        //Pointer pa points to the first element of array a
ndigit=0;        //How many integers does ndigit represent
i=0;             //i represents the first character in the string/
j=0;             //j represents the number of consecutive digits
while(*(pstr+i)!='\0')
{if((*(pstr+i)>='0') && (*(pstr+i)<='9'))
j++;
else
{if (j>0)
{digit=*(pstr+i-1)-48;          //Assign digits to digit
k=1;
while (k<j)     //Accumulate the value of other bits containing more than two digits in digit
{e10=1;
for (m=1;m<=k;m++)
e10=e10*10;                  //e10 represents the factor that this digit should be multiplied by
digit=digit+(*(pstr+i-1-k)-48)*e10;  //Add the number of digits to digit
k++;                         //Digit k self increasing
}
*pa=digit;                     //Place values in array a
ndigit++;
pa++;                         //Pointer pa points to the next element of array a
j=0;
}
}
i++;
}
if (j>0)                         //The last data of a string ending in a number
{digit=*(pstr+i-1)-48;          //Assign digits to digit
k=1;
while (k<j)          // Add the value of other bits containing more than two digits to digit
{e10=1;
for (m=1;m<=k;m++)
e10=e10*10;            //e10 represents the factor that the number of digits should be multiplied by
digit=digit+(*(pstr+i-1-k)-48)*e10;  //Add the number of digits to digit
k++;  //Digit K auto increment
}
*pa=digit;                 //Put values in array a
ndigit++;
j=0;
}
cout<<ndigit<<endl;
j=0;
pa=&a[0];
for (j=0;j<ndigit;j++)            //print data
cout<<*(pa+j)<<endl;
cout<<endl;
return 0;
}
```

## 2. String sorting

[problem description] there are five strings. First, they are sorted by the number of characters in the string, and then the third letter of each string is taken out and combined into a new string output (if the output space is less than three characters). Requirement: using string pointer and pointer array.

[input form] 5 strings, separated by carriage return
[output form] output a string: sort by the number of characters in 5 strings from small to large, then take out the third letter of each string and combine it into a new string output, if the output space is less than three characters

[sample input]

test1234

123test

cumt

think

apples

[sample output]

cumt think apples 123test test1234

concatenate string:mip3s

[sample description] output a space between each string. The relative order of strings with equal number of characters does not change

```#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char *pstr[5];
char str[5][50];
for(int i=0;i<5;i++)
{
pstr[i]=str[i];
}
//Assign a value to a pointer element in a pointer array
for(int i=0;i<5;i++)
{
cin>>pstr[i];
}
//Enter 5 strings
int i,j;
char *k; //Defining pointers for exchange
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(strlen(pstr[j])>strlen(pstr[j+1]))//String length comparison
{
k=pstr[j];
pstr[j]=pstr[j+1];
pstr[j+1]=k;
}

}
}
//Bubble sort
for(int i=0;i<5;i++)
{
cout<<pstr[i]<<' ';
}
//Output after sorting
cout<<endl<<"concatenate string:";
for(int i=0;i<5;i++)
{
if(*(pstr[i]+2))cout<<*(pstr[i]+2);
else cout<<' ';
}
return 0;
}
```

## 3. Insert string

[problem description] input a string from the keyboard, and insert the string "ab" after the largest element in the string for the first time.

[input form] arbitrary input of a string

[output form] insert the string "ab" after the largest element in the string

[sample input]

123csCUMT

[sample output]

123csabCUMT

[sample description] to ensure that the input string has spaces, please use cin.getline (char *, int); do not use gets, which is not supported in this system. Insert ab only once.

```#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char *pstr,str[50];
pstr=str;
cin.getline(str,50);
int len,max=0;//len value string length, max is the position of the largest element
len=strlen(str);
for(int i=0;i<len;i++)
{
if(*(pstr+i)>*(pstr+max))max=i;
}
//Find the location of the largest element
for(int i=len;i>max;i--)
{
*(pstr+i+2)=*(pstr+i);
}
str[max+1]='a';
str[max+2]='b';
cout<<str;
}
```
Published 6 original articles, praised 0 and visited 25

Topics: less