string help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 66
Reputation: bigben09 is an unknown quantity at this point 
Solved Threads: 0
bigben09 bigben09 is offline Offline
Junior Poster in Training

string help

 
0
  #1
Feb 19th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 1
Reputation: harleyrica is an unknown quantity at this point 
Solved Threads: 0
harleyrica harleyrica is offline Offline
Newbie Poster

Re: string help

 
0
  #2
Feb 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string help

 
0
  #3
Feb 19th, 2007
What I would do is flip the number like you mentioned, but instead convert the number back to an integer with a stringstream.

  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...
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 66
Reputation: bigben09 is an unknown quantity at this point 
Solved Threads: 0
bigben09 bigben09 is offline Offline
Junior Poster in Training

Re: string help

 
0
  #4
Feb 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 66
Reputation: bigben09 is an unknown quantity at this point 
Solved Threads: 0
bigben09 bigben09 is offline Offline
Junior Poster in Training

Re: string help

 
0
  #5
Feb 19th, 2007
o and yes C++ string
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string help

 
0
  #6
Feb 19th, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 66
Reputation: bigben09 is an unknown quantity at this point 
Solved Threads: 0
bigben09 bigben09 is offline Offline
Junior Poster in Training

Re: string help

 
0
  #7
Feb 19th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string help

 
0
  #8
Feb 19th, 2007
Originally Posted by bigben09 View Post
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.
  1. string reversedNumbers;
Then instead of printing out the character like you did here:
  1. cout << (integer[cntr--]);
Put it in the string like so:
  1. reversedNumbers += integer[/* you know what goes here*/];
Then pop it into a stream and back out like I showed in the first example.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 66
Reputation: bigben09 is an unknown quantity at this point 
Solved Threads: 0
bigben09 bigben09 is offline Offline
Junior Poster in Training

Re: string help

 
0
  #9
Feb 19th, 2007
ok i have done all that and now i cant get it to print out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string help

 
0
  #10
Feb 19th, 2007
How about showing us your updated code. And please use code tags this time around.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC