| | |
string help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 66
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Mar 2005
Posts: 1
Reputation:
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:
best,
Harley
ex:
C Syntax (Toggle Plain Text)
char* removeTrailingZero(char* number){ int i = strlen(number)-1; while (number[i]=='0' && i>0) i--; number[i] = '\0'; return number; }
Harley
What I would do is flip the number like you mentioned, but instead convert the number back to an integer with a stringstream.
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...
C++ Syntax (Toggle Plain Text)
std::stringstream myStream; int completeNumber; myStream << backwardsNumber; myStream >> completeNumber;
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Feb 2007
Posts: 66
Reputation:
Solved Threads: 0
hey could u show me how that would fit in my code. that would help me alot
c Syntax (Toggle Plain Text)
#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;
Last edited by WaltP; Feb 21st, 2007 at 3:08 am. Reason: CODE TAGS!!!! PLEASE!!! Next time it's an infraction... See RULES
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.
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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
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.
First create a string that will hold the reversed numbers.
C++ Syntax (Toggle Plain Text)
string reversedNumbers;
C++ Syntax (Toggle Plain Text)
cout << (integer[cntr--]);
C++ Syntax (Toggle Plain Text)
reversedNumbers += integer[/* you know what goes here*/];
"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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Strings
- Next Thread: can some one take a look at my program and help me
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






