0

So for this program the user gives a text and it must print the text back modifying in these ways by adding and removing space between words:

  1. Each line should contain 60 characters and be aligned left and right
  2. In each line the biggest space between two words must be bigger than the smallest by only one character and a bigger space must be righter than a smaller one. This is what I have managed to do yet, but without any success.

I tried C++ strings instead of a char array but my knowledge about them is limited yet.

#include <iostream>
using namespace std;

bool isletter(char c) {
  return c >= 'a' and c <= 'z' or c >= 'A' and c <= 'Z';
}

int main() {
  int c, i = 0, constant = 0, counter = 0;
  char text[1500], original[1500];

  do {
    c = getchar();
    original[i] = c;
    i++;
  } while (c != EOF);

  for (int j = 0; j <= i; j++) {
    if (original[j] == ' ') {
      int n = 0;
      j = constant;
      while (not isletter(original[j])) {
        n++;
        text[constant] = original[j];
        j = j + n;
      }
      counter++;
    } else if (text[j] == '\n') {
      text[j] = ' ';
      counter++;
    } else {
      text[j] = original[j];
      counter++;
    }
  }
  for (int i = 0; i < counter; i++) {
    cout << text[i];
  }
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • `constant` is never initialized. Are you getting a warning from your compiler about that? If not, crank up your warnings. – Stephen Newell Jan 16 '22 at 04:57
  • C or C++? Tags say one, title says both. – Mad Physicist Jan 16 '22 at 04:59
  • @StephenNewell I don't get any waring but I fixed it – Dimitris Kurtis Jan 16 '22 at 05:16
  • @MadPhysicist I just edited it to only c++ – Dimitris Kurtis Jan 16 '22 at 05:17
  • What have you learned stepping through your code with a debugger? – Stephen Newell Jan 16 '22 at 05:21
  • Do you have an *idea* for how to do this, an algorithm? Can you tease the problem apart into simpler problems? – Beta Jan 16 '22 at 05:23
  • @Beta at first I want to erase all the extra unnecessary spaces between words and convert '\n' to ' '. I'm having trouble with the algorithm that reads the text and with the one that makes each sentence have 60 characters – Dimitris Kurtis Jan 16 '22 at 05:30
  • Here is code that will read the text: `cin.getline (original, 1500);`. Imagine that you have code that will remove "unnecessary" spaces (whatever that means). What is your plan? – Beta Jan 16 '22 at 05:44
  • Without going into detail on the specific nuances of your question, I want to point you to two answers I've recently written where I go into extensive detail into how to modify strings, char arrays, arrays, etc, in both C and C++. I think you'll be able to learn enough from these two answers to have a better chance at fully solving your problem. See them here ([Removing all the vowels in a string in c++](https://stackoverflow.com/a/70714763/4561887)) and here ([Removing elements from array in C](https://stackoverflow.com/a/70043744/4561887)). – Gabriel Staples Jan 16 '22 at 05:50
  • Why do you think dealing with character arrays is any easier than dealing with strings? If that were the case, we wouldn't need strings. – n. m. could be an AI Jan 16 '22 at 07:34
  • @n.1.8e9-where's-my-sharem., is that question to me or the OP? If to me, I use C++ `std::string` in most cases in my first link. – Gabriel Staples Jan 16 '22 at 07:49
  • 1
    @GabrielStaples to OP – n. m. could be an AI Jan 16 '22 at 07:50

0 Answers0