How to remove white space on a vector pointer?

Asked

Viewed 199 times

0

I have a data entry where the user will enter a certain amount of char, this quantity comes from the first entries of the size of the vectors according to the user’s choice...

How the input of characters will work?

  • The user will type in a single line all n characters requested, for example: ABCDEFGHJILMN
  • After user input each char will be in one index of *ponteiro

What is going on?

I can’t control data entry so the user doesn’t enter blank characters, for example:

Should he enter A B C D, whitespace will be stored in each index of the vector

What I’m trying to do?

According to the above written situation, I decided to get the idea to check which index contains the blank space, and after this delete (de-locate) the memory and leaving the same vector with a new amount of data and only with the char nonwhite...

NOTE [IMPORTANT]:

Default functions of languages cannot be used for vector handling (search, pertinence, insertion, deletion, sorting, etc.).

How could I do this in an elegant way and using good practice?

Just follow my algorithm:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <limits>

using namespace std;


void cleanScreen() {
    system("CLS");
}

int validateDataEntry(istream& strm, int sze, char phrase[]){
    char inputTester;
    strm.get(inputTester);

    if(sze < 0) {
        cout << "============== PROGRAMA FINALIZADO! ==============" << endl;
        exit(0);
    } else {
        if(strm.fail() || sze < 0 || inputTester == '.') {

            while(true) {
                char inputTesterTwo;
                cleanScreen();

                cout << "VALOR DIGITADO INVALIDO.. UTILIZE APENAS NUMEROS POSITIVOS & INTEIROS!" << endl;
                cout << phrase << endl;
                strm.clear();
                strm.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                strm >> sze;
                strm.get(inputTesterTwo);

                if(!strm.fail() && sze > 0 && inputTesterTwo != '.') {
                    cleanScreen();
                    return sze;
                }

                if(sze < 0) {
                    cout << "============== PROGRAMA FINALIZADO! ==============" << endl;
                    exit(0);
                }
            }
        } else {
            return sze;
        }
    }
}


int main()
{
    char *vetorA, *vetorB, *vetorC;
    int sizeN = 0, sizeM = 0, sizeK = 0;

    // ENTRADA DO VETOR A
    cout << "DIGITE A QUANTIDADE DO [VETOR A]:" << endl;
    cin >> sizeN;

    sizeN = validateDataEntry(std::cin, sizeN, "DIGITE A QUANTIDADE DO [VETOR A]:");

    // ENTRADA DO VETOR B
    cout << "DIGITE A QUANTIDADE DO [VETOR B]:" << endl;
    cin >> sizeM;

    sizeM = validateDataEntry(std::cin, sizeM, "DIGITE A QUANTIDADE DO [VETOR B]:");

    // ENTRADA DOS CARACTERES DO VETOR A
    vetorA = new char[sizeN];

    cout << "DIGITE [" << sizeN << "]" << " CARACTERES PARA O [VETOR A].." << endl;
    cin.get(vetorA, sizeN + 1);

    for(int i = 0; i < sizeN; i++) {
        if (isspace(vetorA[i])) {
            delete[] vetorA;
        }
    }


    return 0;
}
  • 1

    If it’s c++ I think you’re complicating in several ways, not just by not using std::string how to force the non-use of language functions. What you want to do is to remove_if(str.begin(), str.end(), isspace); assuming that str is a std::string.

  • 1

    generally speaking " my algorithm" is not used, unless you have created a new algorithm in some area of computer science...in which case the most correct is to say "my program"

2 answers

0

You could do it like this:

char c;
int i = 0;

while (1)
{
    while((c = getchar()) == ' ')
        continue;
    if(c == '\n' || i == n)
        break;
    buffer[i] = c;
    i++;
}

The while internal skips any blank spaces entered by the user.

0


I can’t control data entry so the user won’t enter characters in white, for example: If it enters A B C D, the whitespace will be stored in each index of the vector.

If that’s just the problem, here’s an example of data entry ignoring spaces.
I am using vector because it simply makes no sense to use pointers and new in this case, but it is possible to adapt the answer if it is forbidden to use vector.

#include <iostream>
#include <vector>
using namespace std;

// ---

static void perform_data_entry(vector<char>& chars, int n)
{
  char ch;

  cout << "* digite " << n << " caracteres diferentes de espaco\n";

  for (int i = 0; i < n;)
  {
    cin.get(ch);

    if (isspace(ch))
      continue;

    chars.push_back(ch);
    ++i;
  }

  cout << "* ok, digitacao concluida\n";
}

// ---

int main()
{
  int sizeA;
  vector<char> vetorA;

  cout << "* qual e' a quantidade de caracteres ? ";
  cin >> sizeA;

  perform_data_entry(vetorA, sizeA);

  cout << "*\n";
  cout << "* caracteres digitados:[";

  for (auto ch: vetorA)
    cout << ch;

  cout << "]\n";
  cout << "*\n";

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.