C language: the content of the character array is & quot; student a am I & quot;, and the content is changed to & quot; I am a student & quot;. Only limited space can be opened.

Source: Internet
Author: User

C language: the content of the character array is: "student a am I", and the content is changed to "I am a student". Only a limited space can be opened.

# Include <stdio. h> void my_reverse (int len, char arr []) {int left = 0; int right = len-1; while (left <right) {char tmp = arr [left]; arr [left] = arr [right]; arr [right] = tmp; left ++; right -- ;}} int main () {char arr [] = "tneduts a ma I"; int len = sizeof (arr)/sizeof (arr [0])-1; my_reverse (len, arr ); printf ("% s \ n", arr); return 0 ;}

Returns the reverse output of the string. First, sizeof is different from strlen. The length is determined by the length of the string ending sign \ 0. The true String Length len is = sizeof (arr)/sizeof (arr [0]). -1; len-1 is also used when determining the right length. The void type here is because it only replaces the string address and does not perform other transformations, so no return value is required.

Related Article

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.