简体   繁体   中英

C++ simple string program

beginner here

i wrote the below in C++, it's a short program that currently takes 2 words as inputs, and outputs the same words back but the words are split into even and odd instead. I would like to be able to do this for 'T' words instead, but I can't figure it out. I would like to be able to first input the number of words that will follow, for example 10. Then to input the words and get T results back. So instead of just 2 words, an unlimited amount with the user specifying.

I need to put the below into a function and go from there sometime, but I want to learn the best technique to do so - any advice please?

Thanks! Alex

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;


int main() {
    int T;
    cin >> T;

    string FirstWord;
    cin >> FirstWord;
    int LengthFirst;
    LengthFirst = FirstWord.length();        
    string EvenFirst;
    string OddFirst;
    for (int i = 0; i < LengthFirst; i += 2){
        EvenFirst = EvenFirst + FirstWord[i];
    }
    for (int i = 1; i < LengthFirst; i += 2){
        OddFirst = OddFirst + FirstWord[i];
    }
    string SecondWord;
    cin >> SecondWord;
    int LengthSecond;
    LengthSecond = SecondWord.length();
    string EvenSecond;
    string OddSecond;
    for (int i = 0; i < LengthSecond; i += 2){
        EvenSecond += SecondWord[i];
    }
    for (int i = 1; i < LengthSecond; i += 2){
        OddSecond += SecondWord[i];
    }

    cout << EvenFirst << " " << OddFirst << endl;
    cout << EvenSecond << " " << OddSecond << endl;

    return 0;
}

Think I got it here, I was over-thinking this one

I put it in a for loop, as below - so any number of words can be input, user has to input the number of test cases at the

 #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <string> using namespace std; int main() { int T; cin >> T; for (int i = 0; i < T; i++){ string FirstWord; cin >> FirstWord; int LengthFirst; LengthFirst = FirstWord.length(); string EvenFirst; string OddFirst; for (int i = 0; i < LengthFirst; i += 2){ EvenFirst = EvenFirst + FirstWord[i]; } for (int i = 1; i < LengthFirst; i += 2){ OddFirst = OddFirst + FirstWord[i]; } cout << EvenFirst << " " << OddFirst << endl; } return 0; }

Ultimately, you are performing the same task N times.

First, let's discuss how to store the information. Functionally, we have one string as input which yields two strings as output. std::pair (from <utility> ) lets us easily represent this. But for sake of even-odd, std::array might be a better representation for us. Since we have a variable number of words as input, a variable number of std::array will be output. std::vector (from <vector> ) is our friend here.

Second, let's discuss how to process the information. Using named variables for each output component does not scale, so let's switch to a fixed array (noted below as array<string,2> . By switching to a fixed array for output, addressing each split becomes a function of the loop index ( index % 2 ). Below is a solution that generalizes on a known split size at compile time.

#include <string>
#include <array>
#include <vector>
#include <iostream>

int main() {
  int N;
  std::cin >> N;

  constexpr const int Split = 2;
  using StringPack = std::array<std::string, Split>;
  std::vector<StringPack> output;
  for (int wordIndex = 0; wordIndex < N; ++wordIndex) {
    std::string word;
    std::cin >> word;
    StringPack out;
    {
      int index = 0;
      for (char c : word) {
        out[index % Split] += c;
        ++index;
      }
    }
    output.emplace_back(out);
  }
  for (const auto & out : output) {
    for (const auto & word : out) {
      std::cout << word << ' ';
    }
    std::cout << '\n';
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM