Implementing string Exchanges in string arrays

Source: Internet
Author: User

implementing string Exchanges in string arrays

2015-06-01 Qingdao Zhang Junhao

The implementation of string array string Exchange is divided into three parts:

" 1 "forum posts

" 2 The code idea

" 3 "code, run results

1. forum posts


2. Code Ideas

(1) Two string equal length normal exchange can be;

(2) Two strings are unequal to exchanging character data (including terminators) for short string widths, copying long characters remaining characters to the short string data area (at this time the long string remaining data is still in memory only the Terminator ' / ' split).

3. code, running results

#include <stdio.h>void swap (char *a,char *b) {Char temp = -1;do{       temp = *a;       *a = *b;   *b = temp;   ++a;   ++b;} while (*a! = ' \ \ ' && *b! = ');//When one of the two ends, terminate the interchange if (*a = = ' + ') {//This terminator is not swapped position, the string a ends first, only the remainder of the string B is copied to the string a position.   temp = *a;       *a = *b;   *b = temp;//swap terminator   ++a;   ++b;do{*a++ = *b++;} while (*b! = ');//copy B remaining characters (terminator not copied) *a = *b;//copy Terminator}else{//at this point the Terminator is not swapped position, the string B first end, only the remainder of the string a copy to the string B position is good   temp = *b;       *b = *a;   *a = temp;//Interchange Terminator do{*b++ = *a++;} while (*a! = ');//Copy a remaining character (terminator not copied) *b = *a;//swap Terminator}}void main () {  char a[][7]={"foo", "Bar", "Foobar"}; printf ("Old :%-6s\t%-6s\t%-6s\n ", a[0],a[1],a[2]);    Swap (a[0],a[2]);p rintf ("new:%-6s\t%-6s\t%-6s\n", A[0],a[1],a[2]);}


Implementing string Exchanges in string arrays

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.