C language Enhancement (ix) flipping the order of words in a sentence

Source: Internet
Author: User
Tags strtok

This is a very common topic, very simple, but you use the data structure, or you use the right?


With this problem, you can master:

    • How to output the user input to the console
    • How to split a string
    • How to use data structure correctly


Title: Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters within the word is unchanged.
Words are separated by spaces in sentences. Punctuation is treated like ordinary letters.

For example, enter "I am a student.", then output "student." A am I ".


Ideas

First, get the string

The first thing to be able to get to a string of user input, there are two points to note:

1. If cin<< is used, the output will find that the string after the space is not output , as


Workaround: Use Cin.getline (str,length)

2. Because the user input string length is generally not the same as the length of the character array we created, so if you use the traditional i<str.size () to traverse, you will find that the output of a lot of "hot", because the array is not initialized

Such as


Workaround: Use strlen (str) to get the valid length of the character array.


The following is the code for user input and output

<span style= "FONT-SIZE:14PX;" >char str[100];//input Prompt cout<< "Please input, length less than" <<endl;//user input Cin.getline (str,100);cout<< " The string length entered is: ";//output length Cout<<strlen (str) <<endl;//output content for (int i=0;i<strlen (str); i++) {cout<<str[ I];} Cout<<endl;</span>

Second, flip

After implementing the input and output, it is the key to flip the string.

How to flip? Using the angular index of a character array to transform??? Please, this is not the way we used to learn C language, and for the people who have studied data structure, the first reaction to think of is such a treasure- stack !

Advanced after out, this is the characteristics of the stack! This is in front of C language Hardening (ii) design can be used to find the smallest elements of the stack spoke.

Put the first input string into the stack, the last out of the order is not flipped it?

So what we're going to do is divide the string into substrings, then plug it into the stack and take it out. , Done!


Flip function code

<span style= "FONT-SIZE:14PX;" >void reversestr (char * str) {//intercept, use stack structure to implement flip output char * p; vector<char*> vt;const char * split = ""; p = strtok (str,split); while (P!=null) {//cout<<p;vt.push_back (P);//plug it into the stack P = strtok (null,split);//Continue to split the string}//Use the properties of the stack structure to traverse the while ( Vt.size () >0) {cout<<vt[vt.size () -1]<< ""; Vt.pop_back ();} Cout<<endl;} </span>


Full source code

<span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <stdlib.h> #include <iostream> #include <sstream> #include < Vector>using namespace std;/** flips the order of the words in the sentence. Title: Enter an English sentence, flipping the order of the words in the sentence, but the order of the characters within the word is unchanged. Words are separated by spaces in sentences. For simplicity, punctuation is treated like ordinary letters. For example, enter "I am a student.", then output "student." A am I ". Train of thought 1. The user enters a string of strings and outputs it to 2. Interception, using the stack structure to implement the flip output */void reversestr (char * str) {//intercept, using the stack structure to implement the flip output char * p; vector<char*> VT; const char * split = ""; p = strtok (str,split); while (P!=null) {//cout<<p;vt.push_back (P);//plug it into the stack P = strtok (null,split);//Continue to split the string}//Use the properties of the stack structure to traverse the while ( Vt.size () >0) {cout<<vt[vt.size () -1]<< ""; Vt.pop_back ();} Cout<<endl;} void Main () {/* Input output */char str[100];//input prompt cout<< "Please input, length less than" <<endl;//user input//cin>>str;//no way! Cin.getline (str,100);cout<< "The string length entered is:";//output length Cout<<strlen (str) <<endl;//output//cout<< str<<endl;/*//can't output like this! for (int i=0;i<100;i++) {cout<<str[i];} */for (int i=0;i<strlen (str); i++) {cout<<str[i];} Cout<<endl;reversestr (str); system ("Pause");} </span>

The reason to learn the data structure is that many real problems are satisfying the data structure model. For example, the "flip-stack" of this problem

C language Enhancement (ix) flipping the order of words in a sentence

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.