topic one ,
Given group content is as follows s[] = {1,2,3,4,5,6,7,8,9}, enter a constant m (1<=m<=9), so that the array content order after the n position. If n = 3 o'clock, the contents of the array after 3 positions are {7,8,9,1,2,3,4,5,6}
The code is as follows:
#include <stdio.h>
int main ()
{
int a[] = {1,2,3,4,5,6,7,8,9};
int b[9] = {0};
int *p = A;
int *q = b;
int i,m,n;
printf ("Please enter the number of digits to move: \ n");
scanf ("%d", &m);
n = m;
for (i = 0; i < m i++)
{
*q++ = * (P + 9-n);
n--;
}
for (i = 0;i < 9-m;i++)
*q++ = *p++;
printf ("After moving array: \ n");
for (i = 0; i < 9;i++)
printf ("%d", B[i]);
printf ("\ n");
return 0;
}
The results of the implementation are as follows:
fs@ubuntu:~/qiang/tmp$./zhizhen2
Enter the number of digits to move:
3
After moving the array as:
7 8 9 1 2 3 4 5 6
fs@ubuntu:~/qiang/tmp $./zhizhen2
Please enter the number of digits to move:
7
After moving the array as:
3 4 5 6
7 8 9 1 2 fs@ubuntu:~/qiang/tmp$
Topic Two,
Enter a string with numeric and non-numeric characters, such as a123x456 17960? 302tab5876 the number of consecutive digits as an integer, one at a time in the integer array A, such as 123 into the a[0],456 into a[1, to count the number of integers, and output these numbers;
Analysis: This is a very classic pointer programming problem, there are many solutions on the web, useful malloc, useful sscanf, to find a solution is also important to understand their own, the following is the blogger wrote the program, I hope to help you, the code is as follows:
#include <stdio.h> #include <string.h> int main (int argc, const char *argv[]) {char b[100];
int a[100];
Memset (A, ' n ', 100);
char *p = b;
int i = 0;
Int J;
int sum = 0;
int count = 0; int flag = 1;//flag bit, encountered number 0, encountered Non-numeric 1; Here its initial value is 1, the default first character or non-numeric, not output integer, mainly with the following program printf ("Please input string: \ n");
Gets (b); while (*p) {if (*p <= ' 9 ' && *p >= ' 0 ') {flag = 0;//encounters number, flag=0 sum = sum*10 + *p++-' 0 ';//number of characters The word is converted to an integer and is not output at this time. The next character is not a number, the output} else {while (flag = 0)//At this time read a non-numeric character, judge at this time flag, if the flag is 0. Description The previous character is number {a[i++] = sum;//At this point The digital output, assigned to a[i],i++ sum = 0;//sum 0 flag = 1;//non-numeric characters, flag set 1} p++;//at this time flag to 1, no integer output, then look at the next character}//String end, you will encounter In both cases, one is the last character of the number, the other is a non-numeric character if (flag = 0)//Because the next character of the integer in the previous program is not a number, the integer is output, and if the last one is a number, it cannot be output, so the last character is judged a[i] =
sum;//the last integer output else i--;//at this time the last character is non-numeric, there is no integer output, but I added again, so here i--count = i + 1;//The number of integers is i+1 printf ("Total%d integers \ n", count);
printf ("These integers are: \na[]="); for (j = 0; J < I+1; J +) printf ("%d ", A[j]);
printf ("\ n");
return 0; }
The output results are:
fs@ubuntu:~/qiang/tmp$./zhizhen1
Please enter a string:
123xiao45 ? <er97
A total of 3 integers
these integers are:
a[]=123 45 97
fs@ubuntu:~/qiang/tmp$./zhizhen1
Please enter a string:
xiao12jian5w4gd67dd
Total 4 integers
these integers are:
a[]=12 5 4
fs@ubuntu:~/qiang/tmp$