Change the array "Student a Am I" to "I am a student".

Source: Internet
Author: User

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".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.