Click here to Skip to main content
12,071,437 members (34,859 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.7K views
1 bookmarked
Posted

Swap characters in a string

, 3 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Can't this be done with a simple for loop followed by a single if statement?for(int i=0;i<characters.Length-1;i+=2){ output.Append(characters[i+1]); output.Append(characters[i]);}if (characters.Length % 2 == 1) output.Append(characters[characters.Length - 1]);

Can't this be done with a simple for loop followed by a single if statement?

for(int i=0;i<characters.Length-1;i+=2)
{
    output.Append(characters[i+1]);
    output.Append(characters[i]);
}
 
if (characters.Length % 2 == 1) output.Append(characters[characters.Length - 1]);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

George Swan
Student
Wales Wales
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralOh, no... now I want to write one that uses Reflection to al... Pin
PIEBALDconsult8-Nov-11 18:58
memberPIEBALDconsult8-Nov-11 18:58 
GeneralSlightly different, taking advantage of some binary operator... Pin
Luc Pattyn5-Nov-11 14:22
mvpLuc Pattyn5-Nov-11 14:22 
GeneralReason for my vote of 5 Working as expected. Pin
RaisKazi3-Nov-11 21:38
memberRaisKazi3-Nov-11 21:38 
GeneralYep, That's working Great! Here you get my 5 for this. I hop... Pin
RaisKazi3-Nov-11 21:38
memberRaisKazi3-Nov-11 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160208.1 | Last Updated 3 Nov 2011
Article Copyright 2011 by George Swan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid