-1

i have to make a Convert constructor that takes a string. This string will hold the integers as a string of characters, numbers are separated by the character |. For example, if the parameter is the following string “12|34|56|78”, the resulting object will have the following values: 12, 34, 56, and 78.

for(int i=0; i<8; i++)
{
    if(s[i]!='|')
        cout<<s[i]<<s[i+1]<<", ";

}

this is what ive done so far but the output i get is "12, |2, 34, |3.." how do i make it "12, 34, 56, 78" ?

bolov
  • 72,283
  • 15
  • 145
  • 224
InvaderZim
  • 19
  • 4

7 Answers7

2

As-is you are printing out the | character even when the if condition is true. Using your approach modify the code to:

std::string s = "12|34|56|78";
for (std::size_t i = 0; i < s.size(); i++)  {
    if (s[i] == '|') {
        std::cout << ", ";
    }
    else {
        std::cout << s[i];
    }
}

If you want to modify the string itself then combine the std::string::find and std::string::replace functions as shown in Shreevardhan's answer.

Ron
  • 14,674
  • 4
  • 34
  • 47
2

Simply do a find and replace all

std::string s("12|34|56|78");
for (auto f = s.find('|'); f != string::npos; f = s.find('|')) {
    s.replace(f, 1, ", ");
}
std::cout << s << std::endl;

or, if you have a C-style string and just want the required output

char s[] = "12|34|56|78";

for (int i = 0; s[i]; i++) {
    if (s[i] == '|') {
        cout << ", ";
    }
    else {
        cout << s[i];
    }
}
Ron
  • 14,674
  • 4
  • 34
  • 47
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
1

If you want to change the string with the '|'s to a string with ','s, you can try to use the function replace from std.

std::replace( s.begin(), s.end(), '|', ',');
Lukingan
  • 84
  • 7
0

What you need to create is a string split method:

//Split string -- reusable
vector<string> splitString(string str, string delimiter)
{
  vector<string>v;
  string token;
  size_t pos = 0;

  while((pos = str.find(delimiter)) != string::npos)
    {
      token = str.substr(0, pos);
      v.push_back(token);
      str.erase(0, str.find(delimiter) + delimiter.length());
    }
  v.push_back(str);
  return v;
}

So use the '|' as a delimiter and you'll get the following result :) !

  • This will however result in very ineffective code. – Lundin Nov 20 '17 at 11:58
  • @Lundin Well, assuming the user wanted to just extract the integers....apart from that, why do you think it will be ineffective? Just curious. – Abdush Shakoor Mohamed Nazeer Nov 20 '17 at 12:00
  • 1
    You search through the same string over and over. You possibly resize a vector. You resize the string over and over. All the forced heap reallocations will murder any cache performance and also cause heap fragmentation. The resulting machine code will be horrible, thousands of times slower than a plain strtok() program in C. And no, the compiler can't magically optimize this code into the C equivalent. – Lundin Nov 20 '17 at 12:04
0

If you just use the STL you don't have use a loop.

#include <iostream>
#include <string>
#include <algorithm>
int main() {
    std::string s = “12|34|56|78”;
    std::replace(s.begin(),s.end(),'|',',');
    std::cout << s << '\n';
    return 0;
}

Listen to Stephan T. Lavavej's STL lectures, if you have time. They are fun! Link

anotherone
  • 680
  • 1
  • 8
  • 23
0

Well if you just want a new string with the output you specified, you can do as simple as replacing the delimiter.(you can also copy new string and modify)

#include<bits/stdc++.h>
using namespace std;

int main()
{
string str="12|34|5|100";

 for(int i=0;i<str.length();i++)
 {
  if(str[i]=='|')
    str[i]=',';
 }

    cout<<str<<endl;
    return 0;
}
-1

Have a look at strtok. It's a standard library function.

Citing the source:

char * strtok ( char * str, const char * delimiters );A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters.

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}
yar
  • 1,855
  • 13
  • 26