944,216 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6035
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 19th, 2007
0

string help

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bigben09 is offline Offline
66 posts
since Feb 2007
Feb 19th, 2007
0

Re: string help

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:
  1. char* removeTrailingZero(char* number){
  2. int i = strlen(number)-1;
  3. while (number[i]=='0' && i>0) i--;
  4. number[i] = '\0';
  5. return number;
  6. }
best,
Harley
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harleyrica is offline Offline
1 posts
since Mar 2005
Feb 19th, 2007
0

Re: string help

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

C++ Syntax (Toggle Plain Text)
  1. std::stringstream myStream;
  2. int completeNumber;
  3. myStream << backwardsNumber;
  4. 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...
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 19th, 2007
0

Re: string help

hey could u show me how that would fit in my code. that would help me alot
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctype.h>
  4. #include <fstream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. int reverseDigit, integer, reverse;
  9.  
  10.  
  11. int main()
  12. {
  13.  
  14. string integer;
  15. int cntr;
  16. cout <<"Please input an integer: ";
  17. cin >> integer;
  18.  
  19. cntr = integer.size();
  20. cntr--;
  21.  
  22. cout <<"reverse = ";
  23. if (integer [0] == '-')
  24. {
  25. cout << "-";
  26. }
  27. while (cntr > -1)
  28. {
  29. if (integer [cntr] == '-')
  30. cntr--;
  31. else
  32. cout << (integer[cntr--]);
  33. }
  34. cout << endl;
  35. return 0;
Last edited by WaltP; Feb 21st, 2007 at 3:08 am. Reason: CODE TAGS!!!! PLEASE!!! Next time it's an infraction... See RULES
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bigben09 is offline Offline
66 posts
since Feb 2007
Feb 19th, 2007
0

Re: string help

o and yes C++ string
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bigben09 is offline Offline
66 posts
since Feb 2007
Feb 19th, 2007
0

Re: string help

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 19th, 2007
0

Re: string help

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bigben09 is offline Offline
66 posts
since Feb 2007
Feb 19th, 2007
0

Re: string help

Click to Expand / Collapse  Quote originally posted by bigben09 ...
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.
C++ Syntax (Toggle Plain Text)
  1. string reversedNumbers;
Then instead of printing out the character like you did here:
C++ Syntax (Toggle Plain Text)
  1. cout << (integer[cntr--]);
Put it in the string like so:
C++ Syntax (Toggle Plain Text)
  1. reversedNumbers += integer[/* you know what goes here*/];
Then pop it into a stream and back out like I showed in the first example.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 19th, 2007
0

Re: string help

ok i have done all that and now i cant get it to print out.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bigben09 is offline Offline
66 posts
since Feb 2007
Feb 19th, 2007
0

Re: string help

How about showing us your updated code. And please use code tags this time around.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Strings
Next Thread in C++ Forum Timeline: can some one take a look at my program and help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC