Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,556 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,609 people online right now. Registration is fast and FREE... Join Now!




String class help

 
Reply to this topicStart new topic

String class help

ibaraku
28 Oct, 2007 - 06:09 PM
Post #1

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 176



Thanked: 2 times
My Contributions
I am trying to write code to replace dashes from a file that contains a list of artist and songs, the some of the lines in the files contain multiple dashes. I'm trying to remove all dashes except for the last one, but I'm stuck. The lines look like so
strawberry- fields- The beatles
etc...
here's my code
CODE

string Readline::removeMultDash(){
    int j;
    string str1, dash;
    for(j=0; j<line.length(); j++){
        str1=str1+line[j];
        dash=str1.rfind('-');
        if(line[j]=='-'){
        ;//do nothing since it found the last dash
        }//end if
        else{
            if(line[j]=='-'){
                str1.replace('-', ' ');
            }//end if
        }//end else
            
    }//end for
    return str1=str1+line[j];
}

any kind of help is appreciated, thanks guys
User is offlineProfile CardPM
+Quote Post

Pontus
RE: String Class Help
28 Oct, 2007 - 11:53 PM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
Your code is a bit unorganised
here is some pseudocode

CODE
make an integer called lastdash
create 2 for loops
1 to find the last dash
another 1 to delete all the dashes except the last 1


CODE


string Readline::removeMultDash()
{
int lastdash;
for(int a=0;a<line.length();a++)
{
  if(line[a]=='-')
   lastdash=a;
}
for(int a=0;a<line.length();a++)
{
   if((line[a]=='-')&&(a!=lastdash))
   {
    line[a]==' ':
   }
}
}


This post has been edited by manhaeve5: 29 Oct, 2007 - 10:10 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: String Class Help
29 Oct, 2007 - 09:41 AM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Nice solution there manhaeve5! Only comment is that you have a colon in there where a semicolon should be (in second for loop's if statement).

ibaraku, since DIC is always about expanding your mind I have also created a quick little method you can find useful to accomplish the same thing. So you have a choice to go with either. It is up to you. smile.gif

CODE

#include <iostream>
#include <string>
using namespace std;

int main (){

    // Create our line of text
    string line = "strawberry- fields - The Beatles";

    // Set a flag to determine if last character
    bool lastdash = false;

    // Find our last dash
    size_t i = line.rfind('-');

    // Loop through while we have dashes being found
    while (i != string::npos){
        // If it was the last dash, skip it, otherwise replace with space.
        if (!lastdash) { lastdash = true; }
        else { line[i] = ' '; }
        
        // Decrement our found value so the rfind will find next match
        i--;
        i = line.rfind('-',i);
    }

    cout << "Result is: " << line << endl;


    return 0;
}


Using rfind we start from the end and move through the string, skipping the first instance we find (which is actually the last dash in the string) and replace all the others with your space. Just read through the comments to see how I did everything.

Enjoy! smile.gif
User is offlineProfile CardPM
+Quote Post

Pontus
RE: String Class Help
29 Oct, 2007 - 10:10 AM
Post #4

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
Thanks for alerting me Martyr2

This post has been edited by manhaeve5: 29 Oct, 2007 - 10:11 AM
User is offlineProfile CardPM
+Quote Post

ibaraku
RE: String Class Help
29 Oct, 2007 - 10:46 PM
Post #5

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 176



Thanked: 2 times
My Contributions
Thanks for your help guys!!!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month