Title: The given string "Student a Am I" requires the output "I am a student". (Requires a space complexity of 1, do not use library functions)
Topic Analysis: Student a am I
I ma a tneduts (overall inversion of strings)
I am a student (inversion of each word in the string)
When the whole is reversed, it must not be reversed, such as once reversed, the first occurrence of the top, the character will not output.
Code Show:
#define _crt_secure_no_warnings 1 #include <stdio.h> void reverse (char *left, char *right) {char tmp;
while (left < right) {tmp = *left;
*left = *right;
*right = tmp;
left++;
right--;
} void Reverse_str (char arr[], int sz) {char *left = arr;
Char *right = arr + sz-1;
char *start = arr;
Char *end = arr;//in order to retain the starting and ending positions reverse (left,right);
while (*end!= ' ") {while (*end!= ' &&*end!= '") {end++; }//out of this while loop, it must have encountered a space (that is, the end of a word) or a reverse (the entire string end) (start,end-1), and/or, because it is either a space or a "," Minus 1 if (*end!= ' ")
/The program does not need to perform backwards {start = end + 1;//start to point to the next position of the element, or start;//end, starting at that location, and traversing the}} int main ()
{char arr[] = "Student a Am I";
int len = sizeof (arr)/sizeof (arr[0])-1;//computes the length of the character array, excluding the reverse_str (Arr,len);//calls the reverse function printf ("%s", arr);
System ("pause");
return 0; }
Experience: In fact, I did not think of this method, my approach: to create an array of the same size, from the end of the source string before, a copy, when the space is encountered or a, the entire string copy, and then copy the space, of course, the last word after the copy of the. I do this, First of all, there is no space complexity of 1, and this is not easy to achieve.
The reason why I write my thoughts and practices is to use CSDN to record the mistakes I have made, and then when I see this article, I will think of the mistakes that have been done so that I will never make them again.
Show the code, is the teacher explained after I sorted, the program is easy to write the wrong place, such as while loop control statements (in the code has comments), to reverse the size of the string and so on, hope to pay more attention ~ ~