C++ Retrieve Part of String

Hello everyone,

Is it possible for a console app to retrieve part of a string according to what the user inputs, for example:

The string is: "hello my name is muhasaresa"

If the user wants to extract characters 3-5 , is it possible for the application to extract "llo" and then save it to a seperate string?

Thanks,

Muhasaresa
closed account (DSLq5Di1)
http://www.cplusplus.com/reference/string/string/substr/
Thanks :D

When looking at your links, I also found http://www.cplusplus.com/reference/string/string/replace/

So I was wandering is it possible to use string::replace to do something like decrypt a substitution cypher:

a = 128.
b = 70.
c = 88.

"128.88.128.70." use string replace and get "acab"

Muhasaresa
1
2
3
4
5
6
7
8
found = 0;
  while (found!=string::npos)
    foundNext = str.find_first_of(".", found+1 ); //numbers between two dots

    /// HERE YOUR TRANSFORMATION TAKES PLACE 

    replace( found, foundNext - found, MY_REPLACEMENT_CHAR );
    found = foundNext
something like this maybe?
Thanks so much :D

Muhasaresa
Btw...it is not correct the way I stated it...since the length of your string is changing, found = foundNext is already depreciated here
Hi, when I tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

int main ()
{
    
    
  string str ("128.88.70.128.40.");
  size_t found;

  found=str.find_first_of("128.");
  while (found!=string::npos)
  {
    str[found]='a';
    found=str.find_first_of("128.",found+1);
  }

  cout << str << endl;
  
  
  cin.clear();
cin.ignore(255, '\n');
cin.get();

return 0; 

  return 0;
}


I got "aaa.aa.70.aaa.88." but I was expecting "a88.70.a40."


How can I fix this? I would appreciate your help :)

Muhasaresa

P.S. Can I achieve the same thing but without using full stops, so:

"128887012840" goes to a8870a40
Try this, might help as a starter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
//#include <Windows.h>
// we use something more decend: linux ;)

using namespace std;

int main ()
{    
  string str ("128.88.70.128.40.");
  string searchFor ("128.");
  string replaceBy ("a");
  
  size_t found;
  
  
  found = str.find( searchFor );
  while (found!=string::npos){
    str.replace( found, sizeof( searchFor ), replaceBy );
    found = str.find( searchFor );
  };

  cout << str << endl;
  
  return 0;
}
Last edited on
It gives: a88.70.a40.

And btw, why did you include windows.h?
Last edited on
This is fantastic :D

I was just wondering if it can do "128." to "a" , "70." to "b" and "88." to "c" all at the same time.

So "128.88.70.128.70" goes to "acbac"

Thanks,

Muhasaresa

P.S. can "128887012840" go to "acbac" even if there are no full stops ?

P.P.S. I'm not really sure why I included windows.h. I'm a beginner so I put it in just in case :D LINUX is very good too :D
Last edited on
So "128.88.70.128.70" goes to "acbac"


- yes there a different ways leading to rome... but all in all it's not an issue

P.S. can "128887012840" go to "acbac" even if there are no full stops ?


mhm...it could...but focus here: 12888 where is the 88 after a twelfe or after the 128? its not clear and you will have problems...another thing...your word folding (how was this called ceasars chiffre or something ancient ;) ) why don't you use chars for chars, like replace every "a" with a "h" and "b" with whatever... then you could do it without full stops since every letter is exactly one byte. Or you'll have to add leading zero's like 128088 which would transform to 128 -> a and 088 -> whatever... but you need some "virtual" full stop then. Like a certain amount of numbers or whatever!
I tried
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <Windows.h>


using namespace std;

int main ()
{    
  string str ("128.88.70.128.40.");
  string searchFor ("128.");
  string replaceBy ("a");

 string searchFor ("70.");
  string replaceBy ("b");

 string searchFor ("88.");
  string replaceBy ("c");
  
  size_t found;
  
  
  found = str.find( searchFor );
  while (found!=string::npos){
    str.replace( found, sizeof( searchFor ), replaceBy );
    found = str.find( searchFor );
  };

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


but that did not seem to work. Can this be fixed?

Thanks,

Muhasaresa
There is nothing a good whack with a hammer won't fix ;)

I won't give you a solution but some ideas here, what you should think about...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <Windows.h> //AGAIN? just joking...


using namespace std;

int main ()
{    
  string str ("128.88.70.128.40.");
  string searchFor ("128.");    // The section beginning here.......
  string replaceBy ("a");

 string searchFor ("70.");
  string replaceBy ("b");

 string searchFor ("88.");
  string replaceBy ("c");     /// .......ending here

/* 
Please take a close look at what you did there! "string" is a TYPE a container, a space in memory or whatever you wanna call it
...now you created such a space in memory and named it "searchFor" afterwards you create another space in memory and named it 
the same...now if you call the name it's not clear which one is meant! Another puzzler: what are you going to do if you want to use 
more and more letters...are you planning to do the whole thingy 26 times?? Its quite some effort isn't it?
*/
  
  size_t found;
  
  
  found = str.find( searchFor );
  while (found!=string::npos){
    str.replace( found, sizeof( searchFor ), replaceBy );
    found = str.find( searchFor );
  };

  cout << str << endl;
  
  return 0;
}
Last edited on
Thanks for your help, I then tried this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <Windows.h> // #include <LINUX_is_the_best.h> :D


using namespace std;

int main ()
{    
  string str ("128.88.70.128.70.");
  string searchFora ("128.");    
  string replaceBya ("a");

 string searchForb ("70.");
  string replaceByb ("b");

 string searchForc ("88.");
  string replaceByc ("c");     

  
  size_t found;
  
  
  
  found = str.find( searchFora );
  while (found!=string::npos){
    str.replace( found, sizeof( searchFora ), replaceBya );
    found = str.find( searchFora );
  };
  
    found = str.find( searchForb );
  while (found!=string::npos){
    str.replace( found, sizeof( searchForb ), replaceByb );
    found = str.find( searchForb );
  };
  
    found = str.find( searchForc );
  while (found!=string::npos){
    str.replace( found, sizeof( searchForc ), replaceByc );
    found = str.find( searchForc );
  };

  cout << str << endl;
  
  cin.clear();
cin.ignore(255, '\n');
cin.get();
  
  
  return 0;
}


and got a partial success. It output only the first 3 character "acb". Do I have to do something to the size_t found; to get it working?

Thanks,

Muhasaresa

P.S. I like your hammer quote :D
Last edited on
Topic archived. No new replies allowed.