Thread: [Dev-C++] Reversing a string word by word - advice on my code please
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Daniel G. <sp...@ho...> - 2003-12-12 10:03:45
|
Hi all,
I'd appreciate some code review on the logic of this function I wrote.
I'm using C# (does Dev support C# ?) (with the csc.exe compiler) to reverse
a string and after a bit of struggling (though an idiotic mistake on my part
- it is really embarassing) I managed it, and would like you to have a look
at the code and tell me if there is a better way of doing it.
The logic is in question here, so don't worry about not knowing C#.
StringManip is my class which has one 'string' member we are interested in,
mCompanyName, which is being reversed.
/* ReverseSentenceByWord() is a function that reverses individual words in a
string. It does this
by looking for spaces and reverses all the letters before that
space.
*/
public void ReverseSentenceByWord(ref StringManip SentenceObj)
{
string Sentence = SentenceObj.mCompanyName;
// This number increases each time the iterator moves forward,
counting the length of the word.
int CharsInWord = 0;
// StartOfWord is a marker for where the start of the word
begins. For example the first word will
// always start at 0 (first element), but the next word may
start 7 characters in (6).
// This is required to reverse only one word at a time.
int StartOfWord = 0;
int EndOfWord = 0;
int Iterator = 0;
// What is the total length of the sentence?
int TotalChars = Sentence.Length;
// While we are not at the end of the string continue to run the
code inside
while(Iterator != TotalChars)
{
// If this is the second time around then the start of the
word variable needs to change
// (otherwise we'd be counting from the first letter every
time).
StartOfWord = Iterator;
// Same for the number of characters in the word.
CharsInWord = 0;
// Count how many characters there are from the current
iterator position to the next space.
// But be sure to guard against indexing the end of the
array.
while(Sentence[Iterator] != ' ')
{
if (Iterator == TotalChars-1)
{
// We are on the last letter so to catch it we must
increase CharsInWord.
CharsInWord += 1;
break;
}
// Look at the next character
Iterator += 1;
CharsInWord += 1;
}
// In the case of a double space the character before will
be ' ' so guard against this first of all
// by saying "if the previous char is a space then loop
again".
if ( !((Iterator-1) == ' ') )
{
// Space found, so mark the end of the word as the
iterator's position minus one (we are sitting on a space
// at the moment). We still have the starting position
stored from the previous loop (or 0 if this is the first time).
EndOfWord = Iterator - 1;
// Perform a quick check to ensure that the CharsInWord
variable is correct.
if (CharsInWord == (EndOfWord - StartOfWord))
{
Console.WriteLine("\n\nNumbers do not match\nThe
number of characters reported in the word does not match the actual
amount.\n");
}
// Store the characters from the start of the word to
the end of the word (using CharsInWord counter).
string Temp = Sentence.Substring(StartOfWord,
CharsInWord);
// We must make a copy of Temp because we will use it in
replacing Temp's letters.
string TempCopy = Temp;
/* Reverse and output to screen.
We do this by replacing the first and last elements,
then the second and second
to last elements, then the third and third to last
elements, etc.
*/
// Make a copy of CharsInWord because we will be using
this copy to count down to the middle.
int CharsInTemp = CharsInWord;
// This variable exists outside the for loop and is
added to one character at a time.
// Chars cannot be converted to string types easily but
this method works.
string ReversedString = "";
// Extract the letters from TempCopy one at a time into
ReversedString.
for(int Loop=0; Loop<Temp.Length; Loop++)
{
// Make sure CharsInTemp becomes element 7 from the
off.
if (CharsInTemp != 0)
{
CharsInTemp--;
}
/* Create many chars and concatenate them together
in the reverse order */
char CurrentChar = Temp[CharsInTemp];
ReversedString += CurrentChar;
Console.WriteLine("{0}", ReversedString);
}
}
// Move the iterator forward one.
Iterator += 1;
// Force some space between word reversal in the window.
Console.WriteLine("\n\n");
}
_________________________________________________________________
Use MSN Messenger to send music and pics to your friends
http://www.msn.co.uk/messenger
|
|
From: Per W. <pw...@ia...> - 2003-12-12 10:17:36
|
1: What happen if string starts with a space? 2: Why not reverse each word inplace instead of needing an extra string? while word length > 1 swap first and first + length - 1 increment start pos decrement word length with 2 /Per W |
|
From: <or...@vp...> - 2003-12-12 13:48:45
|
Hi! Hehe. What is this, some common type of homework or book excersise? :) Some time ago, someone asked *exactly* this question, and at least 2 solutions were supplied. However, the subject of the letter doesn't indicate the question, so no wonder you haven't found it. Anyway, look here: http://sourceforge.net/mailarchive/message.php?msg_id=6318449 for my solution, but there are others as well. -- Greetings, Balázs |
|
From: William Mc C. <WIL...@ms...> - 2003-12-12 15:22:22
|
Hi: I tried it on the following sentence with the same results: A MAN A PLAN A CANAL PANAMA ----- Original Message -----=20 From: OROSZI Bal=E1zs=20 To: dev...@li...=20 Sent: Friday, December 12, 2003 5:51 AM Subject: Re: [Dev-C++] Reversing a string word by word - advice on my = code please Hi! Hehe. What is this, some common type of homework or book excersise? :) Some time ago, someone asked *exactly* this question, and at least 2=20 solutions were supplied. However, the subject of the letter doesn't=20 indicate the question, so no wonder you haven't found it. Anyway, look here: http://sourceforge.net/mailarchive/message.php?msg_id=3D6318449 for my solution, but there are others as well. -- Greetings, Bal=E1zs ------------------------------------------------------- This SF.net email is sponsored by: IBM Linux Tutorials. Become an expert in LINUX or just sharpen your skills. Sign up for = IBM's Free Linux Tutorials. Learn everything from the bash shell to sys = admin. Click now! = http://ads.osdn.com/?ad_id=3D1278&alloc_id=3D3371&op=3Dclick _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: <or...@vp...> - 2003-12-12 20:15:31
|
William Mc Coy wrote: > Hi: > > I tried it on the following sentence with the same results: > > A MAN A PLAN A CANAL PANAMA What do you mean by same results? It gave me this: A NAM A NALP A LANAC AMANAP Isn't this, what supposed to be done? -- Greetings, Balázs |
|
From: <or...@vp...> - 2003-12-12 20:31:51
|
Alvin Thompson wrote: > <condescending_tone> > methinks it was a joke. if you reverse all the letters in the sentence, > it gives you the same sentence. > </condescending_tone> Hmmm... really %) I didn't it see at first. Maybe native-english speakers are better at this :) -- Greetings, Balázs |