Home c++ At a given line, remove the second and fourth characters

At a given line, remove the second and fourth characters

Author

Date

Category

As I understand it, you need to delete characters, and then you need to shift the string to the number of characters removed. Explain the displacement algorithm in the words that you need to do first


Answer 1, Authority 100%

std :: string str = "12345";
STR.ERASE (STR.BEGIN () + 3);
STR.ERASE (STR.BEGIN () + 1);
STD :: COUT & LT; & LT; STR & LT; & LT; STD :: ENDL;

Remove the second and fourth

std :: string str = "12345";
  STD :: STRING NEWSTR;
  For (size_t i = 0; i & lt; str.length (); ++ i)
  {
    if (i! = 1 & amp; & amp; i! = 3) // here if options to remake a lot
      NEWSTR + = STR [i];
  }
  STD :: COUT & LT; & LT; NEWSTR & LT; & LT; STD :: ENDL;

Answer 2, Authority 100%

Delete cycle? Including in place? Trivial.

# include & lt; iostream & gt;
Using Namespace STD;
// C-Style
Void Del24 (Char * DST, Char * SRC) {
 int i = 0, j = 0;
 for (; src [i]; i ++)
  if (! (i == 2 || i == 4))
   DST [j ++] = src [i];
 DST [j] = 0;
}
INT MAIN ()
{
 String str ("1234567");
  COUT & LT; & LT; STR & LT; & LT; '\ n';
  for (int i = 0, j = 0; i & lt; str.size (); i ++)
   if (! (i == 2 || i == 4))
    STR [J ++] = STR [i];
  Str.Resize (Str.Size () - 2);
  COUT & LT; & LT; STR & LT; & lt; '\ n';
  DEL24 ((Char *) str.c_str (), (char *) str.c_str ());
  Str.Resize (Str.Size () - 2);
  COUT & LT; & LT; STR & LT; & lt; '\ n';
  Return 0;
}

for every taste.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions