M
M
Mag1str2020-06-04 19:55:21
C++ / C#
Mag1str, 2020-06-04 19:55:21

How to cut characters?

I wrote the code, but it doesn't work from the second line.

Two lines are entered from the keyboard. Create a third line consisting of the last two characters of the first line and the first two characters of the second line.

#include <iostream>
#include <cstring>
#include <locale.h>
#include <windows.h>
using namespace std;

int main()
{
  SetConsoleCP(1251);
  SetConsoleOutputCP(1251);
  char str1[20]="Ya lyublyu";
  char str2[20]="chutatu knuzku";
  char str3[10];
  
  int p1=8,p2=9,p3=1,i=0;

  for(int j=p1;i<=p2 and i<strlen(str1);i++,j++)
  str3[i]=str1[j];
  
  str3[i]=str2[p3];
  
  for(i=0;i<strlen(str3);i++)
  cout<<str3[i];
  
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
edward_freedom, 2020-06-04
@Mag1str

Use insteadstr1[20]string

string buffer;
  vector<string> words;

  cout << "Введите два предложения: " << endl;
  while (words.size() <= 1 && getline(cin, buffer, '\n'))
  {
    words.push_back(buffer);
  }

  if (words[1].length() >= 4 && words[1].length() >= 4) {
    words.push_back(string(words[0].substr(words[0].length() - 2, 2) + words[1].substr(0, 2)));
  }

  cout << endl << "Все предложения: " << endl;
  for (auto word : words) {
    std::cout << word << endl;;
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question