Topic:
The contents of a character array are : "Student A Am I", please change the contents of the array to "I am a student".
Requirement: Library functions cannot be used. Only a limited space can be opened (the number of spaces is independent of the length of the string).
Topic Analysis:
Because the string given in the topic contains a space character, it is not easy to simply reverse the contents of the array, the problem requires the library function can not be used, and can only open a limited space, can not be related to the length of the string, so you cannot create a same array as the original array to hold the changed string.
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h> #include <stdlib.h> #include <assert.h> int my_strlen (CONST&NBSP;CHAR&NBSP;*STR) // Defines the calculation string length function My_strlen{ int count = 0; while (*str++ ) { count++; } return count;} void reverse_str (char *left, char *right) //define string inverse function { assert (left), assert (right); // The Assert function determines whether it is a blank character while (left < right) { char tmp = *left; *left = *right; * Right = tmp; left++; right--; }} void reverse (char arr[]) { char *pstart = arr; //defining a string start pointer char *pend = arr + my_strlen (arr) - 1; //defining a String end pointer char *pcur = NULL; if ((arr == null) | | (arr[0] == ')) //determine if the string is empty or the first character is a space { return ; } reverse_str (pstart, pend); //inverse of the entire string while (*arr) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&Nbsp; pcur = arr; while (*arr != ' && *arr != ' ') { arr++; } pend = arr - 1; reverse_str (pcur , pend); / /Invert each word if (*arr == ' ') { arr++; } }} int main () { char arr[] = "Student a am i"; printf ("%s\n", ARR); reverse (arr); printf ("%s\n", arr); System ("pause"); return 0; }
This article from "Unintentional persistent" blog, reproduced please contact the author!
Change the array "Student a Am I" to "I am a student".