Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:46:33+00:00 2026-06-10T14:46:33+00:00

I asked a similar question but left out an important detail. I am doing

  • 0

I asked a similar question but left out an important detail. I am doing some text processing on an array of chars (cstring). The input array is copied to the output array except certain characters get changed (e.g. a->b). This is done by using a switch statements. What I want is if two or more of a certain characters are found in a row only one of them gets copied to the new array (so I wouldn’t want two spaces in a row).

This is what I’ve got so far and it works without skipping the two or more in a row of certain characters:

char cleanName[ent->d_namlen];
    for(int i = 0; i < ent->d_namlen; i++)
    {
        switch(ent->d_name[i])
        {

            case 'a' :
                cleanName[i] = 'b';//replace a's with b's (just an example)
                break;
            case ' ' ://fall through
            case '-' :
            case '–' :
            case '—' :
                cleanName[i] = '_';//replace spaces and dashes with spaces
                break;
            ....//more case statments
           default: 
                cleanName[i] = ent->d_name[i];
  }
}

For example if two characters in a row get replaced by underscores, how do I do this? Would I only execute the switch statement if(ent->d_name[i] != previous || (ent->d_name[i] != '-' && ent->d_name[i] != '_' && ent->d_name[i] != ' ')

This may be more of an algorithm question than an implementation specific one.

Example input: abbc–d-e
Output: bbbc_d_e
(for simplicity sake assume ‘a’ is mapped to ‘b’ but really there is more than this)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:46:35+00:00Added an answer on June 10, 2026 at 2:46 pm

    Easiest would be to use std::unique with a custom predicate, after your existing transformation:

    cleanName.erase(std::unique(std::begin(cleanName), std::end(cleanName),
        [](char c, char d) { return c == '_' && d == '_'; }), std::end(cleanName));
    

    For a char array:

    length = std::unique(cleanName, &cleanName[length],
        [](char c, char d) { return c == '_' && d == '_'; }) - cleanName;
    cleanName[length] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked similar question before and got some nice code, but i'm concerned with
I've asked a similar question on structs here but I'm trying to figure out
I asked a similar question yesterday but I included some code that basically took
OK, I asked a similar question before but was too confusing because of my
I asked a very similar question here but since it is such a fundamental
Somebody asked similar question not long ago. But nobody answered comprehensively. Assume I have:
I asked a similar question in Code Review forum but was suggested to ask
I asked a similar question before, but it was answered inadequately so I thought
I asked a similar question yesterday, but recognize that i need to rephase it
I already asked a similar question, PE Header requirements , but I'm not really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.