The C language implementation flips a string

Source: Internet
Author: User

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

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.