1.输入一个字符串,内有数字和非数int字字符,如 a123x456 17960? 302tab5876 将其中连续的数字作为一个整数,依次存放到整型数组a中。例如,123放在a[0]a,456放在a[1],统计共有多少个整数,并输出这些数。
```c
#include <stdio.h>
void array(char *p);
int main() {
char s[32] = {0};
char *p = s;
printf("请输入字符串\n");
gets(s);
array(p);
return 0;
}
void array(char *p){
int s1[32] = {0},count = 0;
while (*p)
{
if(*p <= '9' && *p >= '0'){
while (*p <= '9' && *p >= '0'){
s1[count] = s1[count] * 10 + (*p -'0');
p++;
}
count++;
}
p++;
}
for (int i = 0; i < count; i++) {
printf("%d\t",s1[i]);
}
}
2.从终端输入字符串,如将字符串”I love china“,将其改变为 ”china love i“。(不借助于中间变量)
#include<stdio.h>
#include<string.h>
#define N 32
int swap(char *p, char *q);
int main() {
char s[N] = {0};
gets(s);
char *p = s;
char *q = s + strlen(s) -1;
swap(p, q);
while (*p) {
q = p;
while (32 != *q && '\0' != *q) {
q++;
}
swap(p, q - 1);
p = q+1;
}
puts(s);
return 0;
}
int swap(char *p, char *q) {
while (p < q) {
*p ^= *q;
*q ^= *p;
*p++ ^= *q--;
}
}