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