"C and Pointers" section post-programming exercise solution Reference--6.3

Source: Internet
Author: User

"C and Pointers"--6.3

Topic:

Write a function to reverse-arrange the characters in the argument string.

Function Prototypes:

void Reverse_string (char *string);

Requirements:

Use pointers instead of array subscripts

Do not use any function in the C function library to manipulate strings

Do not declare a local array to temporarily store parameter strings

Solution Code:

#include <stdio.h>voidReverse_string (Char*string){    intI, n=0;  while(*(string+N)! =' /')//count the number of characters in a stringn++; N--;//The number of characters n as the index number should be reduced by 1, that is, from 0 to N-1    if(N >0)//There is no need to invert the number of characters equal to 1 o'clock    {         for(i=0; I<= (n/2); i++)        {            if(I! = (ni)) {CharP//Character Content Exchangep = * (string+H); *(string+i) = * (string+n-i); *(string+n-i) =p; }        }    }}intMain () {CharSource[] ="ABCDEFGH"; printf ("before reverse:\n%s\n", source);    Reverse_string (source); printf ("After reverse:\n%s\n", source);    GetChar (); return 0;}

Note:

1. Count the number of non-' s ' characters in the string first

2. The first character turns to Exchange

"C and Pointers" section post-programming exercise solution Reference--6.3

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.