954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using isspace with white spaces

How do i use the function isspace to take sentences read into an array and make multiple spaces only one space between words? Use an if statement?

if (isspace(aChar) )
cout << aChar;

else (

Not sure what I need here.

matrimforever
Junior Poster in Training
67 posts since Sep 2006
Reputation Points: 26
Solved Threads: 0
 

Are you reading the sentence all at once, or word by word?

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

im trying to take user input in the form of a sentence putting it in an array and then print out changes: Capitalize the first letter, then the rest of the letters are lower case. If there are multiple spaces, make them only one. Maybe I don't have to use isspace?

const int SIZE = 100;
int main()
{
    char sentence[SIZE];
cout << "Please enter a short sentence: " << endl;
cin.getline(sentence, SIZE);

for (int i = 1; i < 100; i++)
        sentence[i] = tolower(sentence[i]);
for (int i = 0; i < 1; i++)
        sentence[i] = toupper(sentence[i]);    
//if (isspace(sentence))
    cout << sentence;
    
return 0;
system("PAUSE");
}
matrimforever
Junior Poster in Training
67 posts since Sep 2006
Reputation Points: 26
Solved Threads: 0
 

Output the characters in a loop. Keep track of the previous character you've output. If it was a space, then if the current character is a space, don't output it.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Not sure how to output using a loop? Can you show me please?

matrimforever
Junior Poster in Training
67 posts since Sep 2006
Reputation Points: 26
Solved Threads: 0
 
Maybe I don't have to use isspace?


Yeah. You dont. Consider the use of std::string and std:stringstream.

Anyway this should do for now.

const int SIZE = 100;
int main()
{
   /*
    *   Remember to initialize char arrays
    */
    char sentence[SIZE] = "";
    std::cout << "Please enter a short sentence: " << std::endl;
    std::cin.getline(sentence, SIZE);

   /*
    *   Remove leading spaces
    */
    int i = 0;
    while( i < SIZE && isspace( sentence[i] ))
    {
        i++;
    };
   /*
    *   Check if the array has finished traversing
    */
    if ( i == SIZE )
        return 0;
   /*
    *   Capitalize the first non-space character
    */
    std::cout << (char)toupper(sentence[i++]);
   /*
    *   Start after the leading spaces
    *   Note that this will leave a trailing space,
    *   but that wont be seen.
    */
    for ( ; i < SIZE; )
    {
        std::cout << (char)tolower(sentence[i]);
        if (isspace( sentence[i] ))
        {
            do
            {
                i++;
            }
            while( i < SIZE && isspace( sentence[i] ) );
        }
        else
        {
            i++;
        }
    }
    return 0;
}
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
Not sure how to output using a loop? Can you show me please?


Simple. Using your code,and formatting it correctly with proper indentation -- please learn this. As your programs grow, they will become unreadable without proper formatting:

const int SIZE = 100;
int main()
{
    char sentence[SIZE];
    cout << "Please enter a short sentence: " << endl;
    cin.getline(sentence, SIZE);

    for (int i = 1; i < 100; i++)
    {    
//      sentence[i] = tolower(sentence[i]);
        cout << tolower(sentence[i]);   // output
    }
    for (int i = 0; i < 1; i++)
    {
//      sentence[i] = toupper(sentence[i]);    
        cout << toupper(sentence[i]);    // output
    }
//  return 0;        // wrong place 

//  system("PAUSE");  // Yech!!!!  Don't use this
    getchar();         // instead...

    return 0;        // right place 
}
WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Here is one that removes the trailing spaces too.

const int SIZE = 100;
int main()
{
   /*
    *   Remember to initialize char arrays
    */
    char sentence[SIZE] = "";
    std::cout << "Please enter a short sentence: " << std::endl;
    std::cin.getline(sentence, SIZE);
    int len = strlen( sentence );
   /*
    *   Remove trailing spaces
    */
    int i = len -1;
    while ( i >= 0 && isspace( sentence[i] ) )
    {
        i--;
    }
   /*
    *   Check if the array has finished traversing
    */
    if ( i < 0 )
    {
        return 0;
    }
    /*
     * Terminate the sentence before the trailing spaces and
     * update the new length
     */
    sentence[ i + 1 ] = 0;
    len = strlen( sentence );
   /*
    *   Remove leading spaces
    */
    i = 0;
    while( i < len && isspace( sentence[i] ))
    {
        i++;
    };
   /*
    *   Check if the array has finished traversing
    */
    if ( i == len )
        return 0;
   /*
    *   Capitalize the first non-space character
    */
    std::cout << (char)toupper(sentence[i++]);
   /*
    *   Start after the leading spaces
    */
    for ( ; i < len; )
    {
        std::cout << (char)tolower(sentence[i]);
        if (isspace( sentence[i] ))
        {
            do
            {
                i++;
            }
            while( i < len && isspace( sentence[i] ) );
        }
        else
        {
            i++;
        }
    }
    return 0;
}
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

If you use C++ strings your life would be much simpler coz then you can do something like:

#include <string> 
 
const std::string whiteSpaces( " \f\n\r\t\v" ); 
 
void trimRight( std::string& str, 
       const std::string& trimChars = whiteSpaces ) 
 { 
    std::string::size_type pos = str.find_last_not_of( trimChars ); 
    str.erase( pos + 1 ); 
 
} 
 

void trimLeft( std::string& str, 
       const std::string& trimChars = whiteSpaces ) 
 { 
    std::string::size_type pos = str.find_first_not_of( trimChars ); 
    str.erase( 0, pos ); 
 } 
 

void trim( std::string& str, const std::string& trimChars = whiteSpaces ) 
 { 
    trimRight( str, trimChars ); 
    trimLeft( str, trimChars ); 
 }
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

How do I remove extra spaces in between?

matrimforever
Junior Poster in Training
67 posts since Sep 2006
Reputation Points: 26
Solved Threads: 0
 
How do I remove extra spaces in between?

In between words, lines, sentences, paragraphs, or characters? Please be explicit. Onlyyou know what you want and we can't read minds if you're not in the room with us.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

U can use a loop like this in WaltP's code..

char newsentence[100];
for (int i = 0,j=0; i < 100;)
    {    
        if (sentence[i] == ' ')
       {
           newsentence[j++]=sentence[i];  
           while(sentence[i]==' ') 
               i++;
        }
        else
           newsentence[j++]=sentence[i++];  
     } 
 
newsentence[j]='/0';
bala24
Junior Poster
125 posts since Oct 2006
Reputation Points: 15
Solved Threads: 11
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You