R
R
romentigo2018-05-14 14:27:53
C++ / C#
romentigo, 2018-05-14 14:27:53

C++ - how to form a new string from words that have a length of more than 6 characters?

You need to enter a line in the console and create a new line, where there will be words longer than 6 characters. The type used is char and char*. I've already reached a dead end in my code and I don't know what to do next.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
  char str_1[500];
  int k = 0, f = 0;
  int quantity;
  int spaces = 0;

  puts("Input a string: ");
  gets_s(str_1);

  char* str_2 = new char[strlen(str_1)];

  for (int i = 0; i < strlen(str_1); i++)
  {
    if (str_1[i] == ' ')
    {
      spaces++;
    }
  }


  for (int i = 0; i < spaces + 1; i++)
  {
    quantity = 0;
    while (str_1[k] != ' ')
    {
      quantity++;
                        k++;
    }
    if (quantity >= 6)
    {
      for (int j = 0; j < quantity; j++)
      {
        str_2[f] = str_1[k - j];
        f++;
      }
    }
    k++;
  }


  puts(str_2);

  system("pause>>void");
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-05-14
@romentigo

https://ideone.com/xiiQxL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question