T:有一个字符数组的内容为:"student a am i",请你将数组的内容改为"i am a student".
要求:
不能使用库函数。只能开辟有限个空间(空间个数和字符串的长度无关)。
倒着打印字符串,并没有改变内容
#include<stdio.h> int main() { int i; char **p; char *a[]={"student","a","m","I"}; for(i=3;i>=0;i--) { p=a+i; printf("%s ",*p); } printf("\n"); return 0; }
2.改变内容
#include <stdio.h> #include <assert.h> int my_strlen(const char * str) { assert(str); int count = 0; while (*str) { count++; str++; } return count; } void reverse_str(char *start ,char *end) { while (start < end) { char tmp = *start; *start = *end; *end = tmp; start++; end--; } } void reverse(char *str) { //1.整体逆序 int len = my_strlen(str); char *start = str; char *end = str + len - 1; reverse_str(start, end); // while (*str) { start = str; while ((*str != ' ') && (*str != '\0')) { str++; } end = str - 1; reverse_str(start, end); if (*str == ' ') str++; } } int main() { char str[] = "student a am i"; // i ma a tneduts // i am a student //旋转字符串 // "abcdef" // "cdefab" // "efabcd" reverse(str); printf("%s\n", str); system("pause"); return 0; } #include <stdio.h> int main() { char arr[5] = "abcd"; char(*p1)[4] = (char (*)[4])&arr; char(*p2)[5] = (char (*)[5])arr; p1++; p2++; return 0; }