1.定义和调用函数,将汉语拼音的姓名改为名姓。
2.定义和调用函数,将N×M矩阵左右对调。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void change(char * s)
{
int sp = 0;
int i = 0;
while (s[i++] != ' ');
sp = i;
char * temp = (char * )malloc(sizeof(char) * sp);
memcpy(temp, s, sp * sizeof(char));
temp[sp - 1] = 0;
strcpy(s, s + sp);
strcat(s, " ");
strcat(s, temp);
free(temp);
}
int main()
{
char name[100] = "Zhang Xiaoming";
change(name);
printf("%s", name);
return 0;
}
Xiaoming Zhang
#include <stdio.h>
void exchange(int * arr, int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n / 2; j++)
{
int t = arr[i * n + j];
arr[i * n + j] = arr[i * n + n - j - 1];
arr[i * n + n - j - 1] = t;
}
}
}
int main()
{
int arr[3][4] = { {1,2,7,8},{3,4,9,1},{5,6,2,7}};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
printf("exchange:\n");
exchange(&arr[0][0], 3, 4);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
return 0;
}
1 2 7 8
3 4 9 1
5 6 2 7
exchange:
8 7 2 1
1 9 4 3
7 2 6 5