473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

inverting a string

hi

I tried to do this, something is not correct.

#include <iostream>
#include <string>
using namespace std;
string inverse(string s)
{

// string inv = "";
// int i = s.size(); // reinterpret_cast no go
// while(i>0){
// inv += s.substr(i,1);
// i--;
// }

string inv = "";
string::iterator begin = s.begin();
string::iterator end = s.end();
(while begin != end){
inv += *begin;
}

}

int main(){

string k = "IRS";
cout << inverse(k) << endl;

}

Aug 18 '06 #1
2 5477
Gary Wessle <ph****@yahoo.comwrites:
hi

I tried to do this, something is not correct.

#include <iostream>
#include <string>
using namespace std;
string inverse(string s)
{

// string inv = "";
// int i = s.size(); // reinterpret_cast no go
// while(i>0){
// inv += s.substr(i,1);
// i--;
// }

string inv = "";
string::iterator begin = s.begin();
string::iterator end = s.end();
(while begin != end){
inv += *begin;
}

}

int main(){

string k = "IRS";
cout << inverse(k) << endl;

}
#include <algorithm>
string str = "sunny";
reverse(str.begin(), str.end())
cout << str << endl;
Aug 18 '06 #2
In article <87************@yahoo.com>, ph****@yahoo.com says...

[ ... ]
string inverse(string s)
The library already provides enough to let you do this in one line of
code quite easily. First of all, when you want to traverse some
collection (and for this purpose, a string qualifies as a collection) in
reverse, you usually want to use a reverse_iterator. Most collections
provide begin() and end(). Those that support reverse_iterators normally
suppose rbegin() and rend() as well.

std::string also has a ctor that takes a pair of iterators as arguments
to specify the contents of the new string.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Aug 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
6
by: arctan | last post by:
Hello Group I was wondering in anyone knew of or could generate a script that takes an input file which consists of one long line of charachters, and would output the file inverted (backwards) ...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
1
by: spalding | last post by:
One issue I've had is successfully searching for regular expression documentation outlining the approach to inverting the test (ie. returning true in the case that the expression is not found)....
1
by: vykkilynn | last post by:
My homework this week involved using the for statement to create loops that would display triangles. The only output we're allowed to use are forms of the print statements... i.e. print(),...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.