C: function: Function: Implements the reverse storage and output of all letters in the character array

Source: Internet
Author: User

The first two days of small test encountered a problem, the establishment of a function, function: To achieve the character array of all the letters in reverse storage and output, initially feel simple with a number array, run a lot of errors found in the format, these are unnecessary errors, now, let's say the code idea: Define a character array if there are n characters, let a pointer variable *p point to the first address, * (p+n-1) is the end address, and then achieve *p and * (p+n-1) value interchange, this way let *q=* (p+n-1) every change, p++, q--, when P>q exits the loop, let's look at the code:

1#include"stdio.h"2 CharChangeChar*p,inty);3 voidMain ()4 {5     Chara[6]="abcdef";6Change (A,6); 7printf"%s", a);8 }9 CharChangeChar*p,inty)Ten { One     Char*Q; A     inttemp;//This side is more prone to wrong, some qualitative thinking, will be defined as char type; -q=p+y-1; -      while(p<q) the     { -temp=*p; -*p=*Q; -*q=temp; +p++; -q--; +         } A}

Here just make n=6, see more clearly, to complicate a bit, we can also enter the letter, want a few on a few, this time I use get () to input, if input n, then how do we know this value of N, we can first define *P1 let P=a, both point to the first address, If you exceed the last address, which is * (P+n) ==0, this gives us a condition to end the loop:

#include"stdio.h"voidMain () {Chara[ +];    Gets (a); Char*p; intI,n; I=0; P=A;  while(* (p+i)! =0) {i++; } N=i; printf ("%d", n);}

The code above can be used to find out the number of letters we enter manually, plus the change () function:

#include"stdio.h"CharChangeChar*p,inty);voidMain () {Chara[ +]; printf ("input letter:\n");    Gets (a); Char*P1; intI,n; I=0; P1=A;  while(* (p1+i)! =0)//exceeds the address result of 0 {i++; } N=i;    Change (a,n); printf ("output:\n%s", a);}CharChangeChar*p,inty) {    Char*Q; inttemp; Q=p+y-1;  while(p<q) {temp=*p; *p=*Q; *q=temp; P++; Q--; }}

Run Result: If the number array is similar.

C: function: Function: Implements the reverse storage and output of all letters in the character array

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.