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

reverse a string ?

dmjpro
2,476 2GB
i want to reverse a string in js .........



plz help me out ......

any help is most welcome .........
Feb 28 '07 #1
8 3849
r035198x
13,262 8TB
i want to reverse a string in js .........



plz help me out ......

any help is most welcome .........
You can write your own function for that using the s.charAt();
Feb 28 '07 #2
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function reverse() {
  3. var text = document.getElementById("rtext").value;
  4. var reversed = "";
  5. for (i=text.length; i>=0; i--) {
  6. reversed = reversed + text.charAt(i);
  7. }
  8. document.getElementById("reversed").value = reversed;
  9. }
  10. </script>
  11. <input type="text" id="rtext" value="insert text"><input type="text" id="reversed" value="The Text Reversed"><input type="button" value="Click Me" onclick="reverse();">
  12.  
Generic Example I wrote for you.
Feb 28 '07 #3
mrhoo
428 256MB
str='abcdefghijklmnopqrstuvwxyz';

alert(str.split('').reverse().join(''));

returns >> 'zyxwvutsrqponmlkjihgfedcba'
Feb 28 '07 #4
iam_clint
1,208 Expert 1GB
wow mr hoo i never even knew of a reverse function, shows how much i know..
Feb 28 '07 #5
acoder
16,027 Expert Mod 8TB
wow mr hoo i never even knew of a reverse function, shows how much i know..
Yes, it's an Array object method, see here. However, I wouldn't have thought of that cryptic solution! Nicely done, mrhoo.
Feb 28 '07 #6
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function reverse() {
  3. var text = document.getElementById("rtext").value;
  4. var reversed = "";
  5. for (i=text.length; i>=0; i--) {
  6. reversed = reversed + text.charAt(i);
  7. }
  8. document.getElementById("reversed").value = reversed;
  9. }
  10. </script>
  11. <input type="text" id="rtext" value="insert text"><input type="text" id="reversed" value="The Text Reversed"><input type="button" value="Click Me" onclick="reverse();">
  12.  
Generic Example I wrote for you.
How about making it even more generic:
Expand|Select|Wrap|Line Numbers
  1. function revStr(str) {
  2. var reversed = "";
  3. for (i=str.length; i>=0; i--) {
  4. reversed += str.charAt(i);
  5. }
  6. return reversed;
  7. }
Now add it to the String object:
Expand|Select|Wrap|Line Numbers
  1. String.prototype.reverse = strRev;
and use as follows:
Expand|Select|Wrap|Line Numbers
  1. var str = 'myString';
  2. alert(str.reverse());
Feb 28 '07 #7
iam_clint
1,208 Expert 1GB
i'm sloppy sometimes :P
Feb 28 '07 #8
dmjpro
2,476 2GB
all of u very impressive ......

but mrhoo is very very impressive .....

all of u lot of thanxxxxxxxxxxxxxxx.............................
Mar 1 '07 #9

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

Similar topics

59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
8
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse,...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
24
by: Sathyaish | last post by:
This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got. #include <stdio.h> #include <stdlib.h> #include...
47
by: sudharsan | last post by:
could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
15
by: rajash | last post by:
Thanks for the additional comments. Here is a solution to an exercise I had problems with. I still don't think it's really what's wanted as it uses a "state variable" n - but I can't see how to...
1
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code:...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.