Problem: for string char* = "abcd EFG h"; Required output "H EFG ABCD"; The whole string is flipped, but the order of each word inside is not flipped.
Thought:<1> get the length of the original string,
<2> records the index of the first space where the string appears, and puts it in an array of shapes, such as the
The space where the spaces appear is 0 5 11, then the record array int b[j]; B[0]=0 b[1] = 5; b[2]=11;
<3> the array b[j] in reverse order for each space that begins to appear, for example
"H" Output "H"
"EFG" Output "EFG"
"ABCD" Output "ABCD"
<4> stitching the above output with Strcat is the purpose string to get
The code is as follows:
#include <iostream> #include <string.h>using namespace std; #define Array_len 10int Main () {char* a= "Opki AM Test abcdef "; int len = strlen (a); cout<< "Original---" <<a<< "---" <<endl; cout<< "str len is" <<len<<endl; int firstempty = false; int B[array_len]; int j = 0; for (int i = 0; i < len; i++) {if (i = = 0) {if (a[i] = = ") {B[j] = 0; j + +; Firstempty = true; }} int temp = i + 1; if (a[i]! = "&& a[temp] = =") {b[j] = temp; j + +; }} cout<< "J is" <<j<<endl; cout<< "---change--"; for (int k = j; k > 0; k--) {int tmp = b[k-1]; int i = 0; BOOL flag = FALSE; while (a[tmp]! = ' + ') {char ch = a[tmp]; if (ch! = ") {flag = true; cout<<ch; } else {if (!flag) {i++; } else {break; }} tmp++; } for (int t = i; t >0; t--) {cout<< "; }} if (!firstempty) {cout<<*a; while (* (++a)! = ") cout<<*a; } cout<< "---" <<endl<< "End" <<endl; return 0;}
The C language implementation flips a string