Home > Backend Development > C++ > body text

How do I remove leading and trailing spaces, and reduce multiple spaces to single spaces in a C string?

Mary-Kate Olsen
Release: 2024-11-13 07:07:02
Original
256 people have browsed it

How do I remove leading and trailing spaces, and reduce multiple spaces to single spaces in a C   string?

Removing Leading and Trailing Spaces from a String in C

This task, commonly known as string trimming, can be accomplished using C 's string class. To address potential extra spaces between words, a separate operation called string reduction is employed.

Trimming Leading and Trailing Spaces

To remove leading and trailing spaces, the trim() function can be defined using the find_first_not_of and find_last_not_of methods:

std::string trim(const std::string& str, const std::string& whitespace = " \t")
{
    const auto strBegin = str.find_first_not_of(whitespace);
    if (strBegin == std::string::npos)
        return ""; // no content

    const auto strEnd = str.find_last_not_of(whitespace);
    const auto strRange = strEnd - strBegin + 1;

    return str.substr(strBegin, strRange);
}
Copy after login

String Reduction

To remove extra spaces between words, the reduce() function performs the following operations:

  1. Trims the string using the trim() function.
  2. Replaces consecutive spaces with a single space using find_first_of, find_first_not_of, and replace.
std::string reduce(const std::string& str,
                   const std::string& fill = " ",
                   const std::string& whitespace = " \t")
{
    // trim first
    auto result = trim(str, whitespace);

    // replace sub ranges
    auto beginSpace = result.find_first_of(whitespace);
    while (beginSpace != std::string::npos)
    {
        const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
        const auto range = endSpace - beginSpace;

        result.replace(beginSpace, range, fill);

        const auto newStart = beginSpace + fill.length();
        beginSpace = result.find_first_of(whitespace, newStart);
    }

    return result;
}
Copy after login

Usage

The following code demonstrates the usage of the trim() and reduce() functions:

int main(void)
{
    const std::string foo = "    too much\t   \tspace\t\t\t  ";
    const std::string bar = "one\ntwo";

    std::cout << "[" << trim(foo) << "]" << std::endl;
    std::cout << "[" << reduce(foo) << "]" << std::endl;
    std::cout << "[" << reduce(foo, "-") << "]" << std::endl;

    std::cout << "[" << trim(bar) << "]" << std::endl;
}
Copy after login

Output

[too much               space]
[too much space]
[too-much-space]
[one
two]
Copy after login

The above is the detailed content of How do I remove leading and trailing spaces, and reduce multiple spaces to single spaces in a C string?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template