Write a program that reads in a sentence of up to 100 characters and outputs the sentence with spacing corrected and with letters corrected for capitalization. In other words, in the output sentence, all strings of two or more blanks should be compressed to a single blank.

I don't know how to compress multiple blank spaces to one blank space. But my other functions work. Please help me with my deleteSpaces function!

#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;

/****************************************************************************
Program: Ch8-1
Purpose: Fix sentences.
****************************************************************************/

const int MAX = 101;

void clearPhrase(char p[], int &size);
void capitalize(char p[]);
void deleteSpaces(char p[], int &size);
void lowercase(char p[]);

int main()
{
    char phrase[MAX], space1, space2, ans;
    int size, i;
    
    do {
         clearPhrase(phrase, size);
         
         cout << "Enter phrase: ";
         cout << phrase;
         cin.getline(phrase, MAX);
         
         capitalize(phrase);
         lowercase(phrase);
         deleteSpaces(phrase, size);
         
            //cout << phrase;
         
         cout << endl << endl << "Do this again? (y/n) ";
         cin >> ans;
         cout << endl;
         
         fflush(stdin);
         
    } while (ans == 'y' || ans == 'Y');
    
    system("PAUSE");
    return 0;
}

void clearPhrase(char p[], int &size) 
{
    p[0] = '\0';
    size = 0;
}

//makes first letter of the sentence capital.
void capitalize(char p[])
{
     if (p[0] != ' ')
        p[0] = toupper(p[0]);
     
}

//deletes extra spaces.
void deleteSpaces(char p[], int &size)
{    
     for (int i = 0; i < strlen(p); i++)
     {    
            if (p[i] != ' ')
            {
                cout << p[i];
            }
     }
}

//makes all letters within the sentence lowercase.
void lowercase(char p[])
{
     for (int i = 1; i < strlen(p); i++)
     {
         p[i] = tolower(p[i]);
     }
}

Recommended Answers

All 2 Replies

Whenever you find a space, delete all spaces that come right after it.
Deleting a character is done by moving all following characters one position down.

Got it! :)

void deleteSpaces(char p[], int &size)
{    
     //used to tell us if there is a blank space.
     bool found = false;
     
     //goes through whole string.
     for (int i = 0; i < strlen(p); i++)
     {    
            if (p[i] != ' ')
            {
                //if a character isn't a space, output it.     
                cout << p[i];
                //tells program that we didn't find it.
                found = false;
            }
            
            if(p[i] == ' ' && found == false)
            {
                //blank space is found, so display only one blank space.
                found = true;
                cout << " ";
            }
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.