473,386 Members | 1,830 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,386 software developers and data experts.

Reversal of a sentence

I'm using Turbo C++ and I have a problem:
how do you reverse the words of a string??
eg:

If i type:
I GO TO SCHOOL

program gives::

SCHOOL TO GO I

-voidmaingetch
Sep 25 '07 #1
8 2240
Nepomuk
3,112 Expert 2GB
I'm using Turbo C++ and I have a problem:
how do you reverse the words of a string??
eg:

If i type:
I GO TO SCHOOL

program gives::

SCHOOL TO GO I

-voidmaingetch
Hi voidmaingetch!
You should ask your question in the C/C++ Forum. The chance of getting an answer quickly is much greater there.

Greetings,
Nepomuk
Sep 25 '07 #2
Ganon11
3,652 Expert 2GB
Moving this thread to the C++ / C Forum...
Sep 25 '07 #3
ilikepython
844 Expert 512MB
I'm using Turbo C++ and I have a problem:
how do you reverse the words of a string??
eg:

If i type:
I GO TO SCHOOL

program gives::

SCHOOL TO GO I

-voidmaingetch
I think the best way to do that is to put all the words into an array and then iterate over the array backwords printing out each word. What have you tried so far?
Sep 25 '07 #4
sicarie
4,677 Expert Mod 4TB
I'd recommend strtok(). I'm not sure the best algorithm for doing this, but you might parse through the string, putting each word in a vector, then you can iterate backwards through the vector and print them out.

Any other implementation ideas? It seems there should be a better one than mine...
Sep 25 '07 #5
ilikepython
844 Expert 512MB
I'd recommend strtok(). I'm not sure the best algorithm for doing this, but you might parse through the string, putting each word in a vector, then you can iterate backwards through the vector and print them out.

Any other implementation ideas? It seems there should be a better one than mine...
I think that strtok() is mostly C and for C++ I think the best way to get the words would be to iterate over a string that you used for getline():
Expand|Select|Wrap|Line Numbers
  1. string line;
  2. getline(cin, line);
  3.  
  4. for (int x = 0; x < line.size(); x++)
  5. {
  6.     // if line[x] is a letter; add to word
  7.     // if line[x] is a space; close word and start new word
  8. }
  9.  
It might be easier with cin though.
Sep 25 '07 #6
sicarie
4,677 Expert Mod 4TB
It might be easier with cin though.
That's a good idea. You can read them in and reverse them on the spot (you know that the first one you read will be the last, etc...)

I think that strtok() is mostly C and for C++
Is the OP not using C++?
Sep 25 '07 #7
Ganon11
3,652 Expert 2GB
That sentence was really two ideas, e.g.:

I think that strtok() is mostly C; for C++...
Sep 25 '07 #8
sicarie
4,677 Expert Mod 4TB
That sentence was really two ideas, e.g.:
Ah, that would make more sense. Yeah, I like strtok() and probably use it more than I should...
Sep 25 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

74
by: Michael | last post by:
As if we needed another string reversal question. I have a problem with the following code, that I believe should work. int StringReverse(char* psz) { char *p = psz; char *q = psz +...
24
by: ineedyourluvin1 | last post by:
Why does this reverse the string ? Sorry about using namespace std ; It made it easier to read :-) Thanks #include<iostream> using namespace std ; void rev_str( char* str ) {
6
by: ashok.anbalan | last post by:
I tried using the string reversal routine that occurs in K & R 2nd edition (Pg. 62) & the program core dumps. #include <stdio.h> #include <stdlib.h> void reverse ( char s ) { int c, i, j;
11
by: Tosha | last post by:
What is the fastest way to convert __int64 type from BigEndian To Little Endian? I wrote this function: --- uint64 EndianReverse(uint64 number) { typedef unsigned __int8 uint8; const int size =...
12
by: Sathyaish | last post by:
How would you reverse a string "in place" in python? I am seeing that there are a lot of operations around higher level data structures and less emphasis on primitive data. I am a little lost and...
25
by: Frederick Gotham | last post by:
I was intrigued by someone the other day who posted regarding methods of "mirror-imaging" the bits in a byte. I thought it might be interesting to write a fully-portable algorithm for...
8
by: gsl | last post by:
Can anyone tell me how to reverse an integer number represented in binary form(or we can say binary reversal of int)? for ex: given 101100 .... the answer should be 001101
5
by: RobertEstelle | last post by:
Hello everyone, I encountered an interesting bug in a program I was writing that was caused by the following unexpected behaviour. Basically, creating a new Stack object from another results in a...
3
by: steezli | last post by:
Hi, Brand new to VB.NET and I'm having a problem figuring out this program. I'll try and be descritive as possible. I have to create a Windows application that contains a single top-level...
144
by: dominantubergeek | last post by:
Hello, I'm a highly experienced expert C programmer and I've written this code to reverse a string in place. I think you could all learn something from it! int reverse(char* reverseme){ int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.