C语言实现''student a am i''字符串的正确排列
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int Reverse(char a[],char b[],int len) { int i = 0; //建立一个新数组用来储存逆序的语言 for (int i = 0; i <= len; i++) { if (a[i] == '\0') { return 0; } b[len-i-2] = a[i]; } return 0; } int ArrangeWords(char a[],char b[], int len) { /*先判断是否多过一个字符 * * 再将多个字符的位置全部交换*/ int left = 0; int right = 0; int j = 0; for (int i = 0; i < len-1; i++) { if (b[i] == ' ') { left = i + 1; a[i] = b[i]; continue; } if (b[i] != ' ') { if ((b[i + 1] == ' ')||(i+2==len)) { right = i; for ( j = left; j <= right; j++) //交换字符 { a[j] = b[left+right-j]; continue; } } continue; } if (b[i] == '\n') { break; } } return 0; } int main(){ char a[] = "student a am i"; char b[] = "student a am i"; int len = sizeof(a) / sizeof(a[0]); for (int i = 0; i < len; i++) { printf("%c", a[i]); } printf("\n"); //逆序 Reverse(a,b, len); //排列单词 ArrangeWords(a,b, len); for (int i = 0; i < len; i++) { printf("%c", b[i]); } printf("\n"); for (int i = 0; i < len; i++) { printf("%c", a[i]); } printf("\n"); system("pause"); return 0; }