상세 컨텐츠

본문 제목

Write A Program To Print Your Name In Reverse Order

카테고리 없음

by smithagplicar1984 2020. 2. 25. 18:06

본문

Write a program to print your name in reverse order word

There are a couple of steps here. First we want to get all the lines from the first file, and then we want to write them in reversed order to the new file. The code for doing this is as follows lines = with open('text.txt') as f:lines = f.readlineswith open('newtext.txt', 'w') as f:for line in reversed(lines):f.write(line)Firstly, we initialize a variable to hold our lines.

Write A Program To Print Your Name In Reverse Order In Java

Then we read all the lines from the 'test.txt' file.Secondly, we open our output file. Here we loop through the lines in reversed order, writing them to the output file as we go. Use it like this if your OS uses n to break lines f = open('text.txt', 'rb')s = f.readf.closef = open('newtext.txt', 'wb')f.write(reversed(s.split('n')).join('n'))f.closeMain thing here is reversed(s.split('n')).join('n').It does the following:. Split your string by line breaks - n,. resulting an array. reverses the array.

merges the array back with linebreaks n to a stringHere the states:. string: line1 n line2 n line3. array: 'line1', 'line2', 'line3'.

array: 'line3', 'line2', 'line1'. string: line3 n line2 n line1 n.

Output:ExplanationIn the above program, the standard input/output header file is included in the program using the preprocessor directive #include. Then a user-defined function, revAString is declared and in its definition, reversing the string using swapping logic is written. First of all, you take g (a counter variable), a variable numb of type integer and a temporary variable (name tmpry) used for swapping and initialize this temporary variable with 0.Next, a for loop is implemented: for(numb=0; strgnumb!= 0; numb); without a body which checks whether the strg is empty or not.

If not, in the next for loop it, iteration will be done from g=0 to g less than half of the value of numb. Inside the block of for-loop, the swapping will be taking place:tmpry = strgg;strgg = strgnumb - 1 - g;strgnumb - 1 - g = tmpry;It is to be noted that for reversing a string of length n, you only need n/2 iterations.

Give More Feedback

Now, once the swapping of strings is done, you need another looping to display the reversed string, which is done using this in our program:for(g = 0; g.