954,198 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

string help

is there a way to tell the program to remove a certain charecter or number from a string with out giving it a position. for example i have a program that asks the user to enter a set of numbers and the program reverses the order of the numbers. so if the user entered 12340 theprogram should print out 4321. but my program prints out 04321. i cant make it not print out the zero when it is at the front. but I need it to print out the zeros when they are at the end. for example if the user enters 01234 the program needs to print out 43210. you see. I need help please.

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

read user's input as string, so your normal reverse should print 43210 when input is 01234. to treat the other case, just remove trailing zeros after you read input before you perform reverse, because they will unwantedly appear in the front later otherwise:

ex:

char* removeTrailingZero(char* number){
    int i = strlen(number)-1;
    while (number[i]=='0' && i>0) i--;
    number[i] = '\0';
    return number;
}

best,
Harley

harleyrica
Newbie Poster
1 post since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

What I would do is flip the number like you mentioned, but instead convert the number back to an integer with a stringstream.

std::stringstream myStream;
int completeNumber;
myStream << backwardsNumber;
myStream >> completeNumber;



When you convert the number back to an integer, any leading 0s will automatically be snipped off.

I'm assuming you meant a C++ string...

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

hey could u show me how that would fit in my code. that would help me alot

#include "stdafx.h"
#include <iostream>
#include <ctype.h>
#include <fstream>
#include <string>
using namespace std;

int reverseDigit, integer, reverse; 


int main()
{

    string integer;
    int cntr;
    cout <<"Please input an integer: ";
    cin >> integer;
        
    cntr = integer.size();
    cntr--;

    cout <<"reverse = ";
    if (integer [0] == '-')
        {
            cout << "-";
        }
    while (cntr > -1)
        {    
            if (integer [cntr] == '-')
                cntr--;
            else 
                cout << (integer[cntr--]);
        }
    cout << endl;
    return 0;
bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

o and yes C++ string

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Probably the best idea would be to store the reversed number in a string before outputting it to the screen on-the-fly. That way you can manipulate the string anyway you want.

Or you could simply do the leading 0s check before you print out the char; you'd need a bool to keep track of whether you've hit a nonzero number, and if that's false, you basically discard any 0s.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

yeah this is al going way over my head i am new to this. could u show me were it would go in my program. that would help me alot. i posted my prograam up top u can run it and see how it works if you would like. thaks

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 
yeah this is al going way over my head i am new to this. could u show me were it would go in my program. that would help me alot. i posted my prograam up top u can run it and see how it works if you would like.


Look, I'm not going to add all the lines of code necessary so that your project is completed without you doing any of the work. I'll show you how to do my first solution, which is to store the numbers in a string, convert it to an integer, and then output the result.

First create a string that will hold the reversed numbers.

string reversedNumbers;


Then instead of printing out the character like you did here:

cout << (integer[cntr--]);


Put it in the string like so:

reversedNumbers += integer[/* you know what goes here*/];



Then pop it into a stream and back out like I showed in the first example.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

ok i have done all that and now i cant get it to print out.

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

How about showing us your updated code. And please use code tags this time around.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

ok how do i use code tags

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 
#include "stdafx.h"
#include <iostream>
#include <ctype.h>
#include <fstream>
#include <string>
using namespace std;


int reverseDigit, integer, reverse; 


int main()
{ 
    string integer, reversedNumbers;
    int cntr;
    cout <<"Please input an integer: ";
    cin >> integer;
  
    cntr = integer.size();
    cntr--;

    cout <<"reverse = ";

    if (integer [0] == '-')
        {
            cout << "-";
        }
    while (cntr > -1)
        {    
            if (integer [cntr] == '-')
                cntr--;
            else 
                reversedNumbers += integer[cntr--];
            //cout << reversedNumbers;
        }
    cout << endl;
    
    return 0;


}
bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

If you had looked at the watermark in the quick reply box, read forum anouncements here, read the rules or even read my signature you would have seen this link somewhere.

http://www.daniweb.com/techtalkforums/announcement8-3.html

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

o and i commented out the cout. because if u run it without the cout uget nothing and if u run it with the cout u get extra numbers, i think it is the position in the string. if u run it both ways u will see what i am talking about.

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Well? What did you expect? C++ isn't just going to automatically print out variables; you have to explicity state that you want them printed out. If programming was that automatic, us programmers would be out of a job.

Anyway, reread my post above. Convert to an integer by using a stringstream, and then print out the value.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

ok and is going to be used before int main or in the program it self, because i am getting an error about it. and I dont no what it is trying to tell me, this is the error error C2079: 'myStream' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
std::stringstream myStream;

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

I forgot to tell you that you need

#include <sstream>

If you want to use the method I mentioned previously.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

ok i am getting an error on the

std::stringstream myStream;



the error is: error C2079: 'myStream' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
were is this need to be in the code above int main or in the code its self?

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

o ok

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

ok i am getting no errors, but it is still not printing out to the screen. i have never used myStream is this print out to the screen or is it just the name of a string i am confused. sorry about all the questions.

bigben09
Junior Poster in Training
66 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You