Ap Computer Science A Using Loops With Strings

String Traversal

String These are things that contain text-based information.

This ability to cross string can be extremely useful.

".<romr<v<N" sa <man hcus htiW ,rood r<bmahc sih <voba tsub d<rutplucs <ht nopu tsa<b ro driB —rood r<bmahc sih <voba drib gni<<s htiw d<ss<lb saw t<y r<v< gni<b namuh gnivil on taht gni<<rga pl<h tonnac <w roF ;<rob ycnav<l<r <lttil—gnina<m <lttil r<wsna sti hguohT ,ylnialp os <sruocsid ra<h ot lwof ylniagnu siht d<ll<vram I hcuM ".<romr<v<N" n<vaR <ht htouQ "!<rohs nainotulP s'thgiN <ht no si <man yldrol yht tahw <m ll<T —<rohs ylthgiN <ht morf gnir<dnaw n<vaR tn<icna dna mirg yltsahG ,n<varc on <rus tra" ,dias I ",uoht ,n<vahs dna nrohs <b ts<rc yht hguohT" ,<row ti <cnan<tnuoc <ht fo muroc<d nr<ts dna <varg <ht yB ,gnilims otni ycnaf das ym gniliug<b drib ynob< siht n<hT

We have been assigned to solve the problem using string traversal. Create a new string with the characters reversed

The first known problem is that the text of the poem is inverted. Declare a new, empty String to store the reversed text. Create a for loop that will iterate through every value of our String. Inside the loop, grab a single element at the String index of the current iteration. Also within the loop, update the new String by prepending the current element’s value to the current String value. Return the new String once the loop is complete.

Leave a Comment

Your email address will not be published. Required fields are marked *