C Language: The contents of the character array are: "Student a Am I", the content is changed to "I am a student". Only a limited space can be opened.

Source: Internet
Author: User

Tag: C: The contents of the character array are: "Student a Am I" is changed to "I am a student". can only open a limited space

#include <stdio.h>void my_reverse (int len, char arr[]) {int left =0; int right = len-1; while (left < right) {CH  AR 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; }

Invert the string output.

First, sizeof is not the same as strlen, which asks for length, including the length of the string ending flag, and the true string length len is

=sizeof (arr)/sizeof (arr[0])-1; Use len-1 when determining the right length.

The void type here is because the problem only changes the string address, and does not do other transformations, so it does not need to return a value.

C Language: The contents of the character array are: "Student a Am I", the content is changed to "I am a student". Only a limited space can be opened.

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.