Hướng dẫn how do you reverse a sentence in javascript? - làm thế nào để bạn đảo ngược một câu trong javascript?

Ví dụ 1: Đảo ngược một chuỗi bằng cách sử dụng cho vòng lặp

// program to reverse a string

function reverseString(str) {

    // empty string
    let newString = "";
    for (let i = str.length - 1; i >= 0; i--) {
        newString += str[i];
    }
    return newString;
}

// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);

Đầu ra

Enter a string: hello world
dlrow olleh

Trong chương trình trên, người dùng được nhắc nhập một chuỗi. Chuỗi đó được chuyển đến hàm

Enter a string: hello world
dlrow olleh
4.

Bên trong hàm

Enter a string: hello world
dlrow olleh
4,

  • Một biến báo chí trống được tạo ra.
  • Vòng
    Enter a string: hello world
    dlrow olleh
    6 được sử dụng để lặp lại trên các chuỗi. Trong lần lặp đầu tiên,
    Enter a string: hello world
    dlrow olleh
    7 đưa ra vị trí của phần tử cuối cùng. Yếu tố đó được thêm vào biến báo chí. Quá trình này tiếp tục cho tất cả các yếu tố chuỗi.
    This process continues for all the string elements.
  • Giá trị của tôi giảm trong mỗi lần lặp và tiếp tục cho đến khi nó trở thành 0.0.

Ví dụ 2: Đảo ngược chuỗi bằng cách sử dụng các phương thức tích hợp

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);

Đầu ra

Enter a string: hello
olleh

Trong chương trình trên, người dùng được nhắc nhập một chuỗi. Chuỗi đó được chuyển đến hàm

Enter a string: hello world
dlrow olleh
4.

  • Bên trong hàm
    Enter a string: hello world
    dlrow olleh
    4,
  • Một biến báo chí trống được tạo ra.
  • Vòng
    Enter a string: hello world
    dlrow olleh
    6 được sử dụng để lặp lại trên các chuỗi. Trong lần lặp đầu tiên,
    Enter a string: hello world
    dlrow olleh
    7 đưa ra vị trí của phần tử cuối cùng. Yếu tố đó được thêm vào biến báo chí. Quá trình này tiếp tục cho tất cả các yếu tố chuỗi.

Tôi đang tạo một chương trình JavaScript trong đó nếu người dùng nhập một sting như

"i love my country"

Đầu ra sẽ tạo ra:

country
my
love
i

Tôi đã thử sử dụng chức năng

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
4 nhưng không nhận được đầu ra cần thiết.


Hướng dẫn how do you reverse a sentence in javascript? - làm thế nào để bạn đảo ngược một câu trong javascript?

Hỏi ngày 4 tháng 12 năm 2015 lúc 8:14Dec 4, 2015 at 8:14

Hướng dẫn how do you reverse a sentence in javascript? - làm thế nào để bạn đảo ngược một câu trong javascript?

5

Hãy thử sử dụng

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
5,
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
6

var str = "I LOVE MY COUNTRY";
console.log(str.split(/\s/).reverse().join(" "));


Loại bỏ

// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
7 tại
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
8;
// program to reverse a string

function reverseString(str) {

    // return a new array of strings
    const arrayStrings = str.split("");
   
    // reverse the new created array elements
    const reverseArray = arrayStrings.reverse();
 
    // join all elements of the array into a string
    const joinArray = reverseArray.join("");
    
    // return the reversed string
    return joinArray;
}
 
// take input from the user
const string = prompt('Enter a string: ');

const result = reverseString(string);
console.log(result);
9 không phải là một chức năng; Thêm
Enter a string: hello
olleh
0 bên trong vòng lặp
Enter a string: hello world
dlrow olleh
6 để bao gồm thẻ
Enter a string: hello
olleh
2 trong đầu ra sau mỗi lần lặp
Enter a string: hello
olleh
3 để đặt đường phạm trong
Enter a string: hello
olleh
4 sau một từ được nhập vào tại
Enter a string: hello
olleh
5. Ví dụ: thử đầu vào của "Tôi yêu đất nước của tôi" tại
Enter a string: hello
olleh
6 tại Stacksnippets

________số 8

Đã trả lời ngày 4 tháng 12 năm 2015 lúc 8:17Dec 4, 2015 at 8:17

1

Hãy thử mã này

var str = "SAMPLE STRING";
alert( str.split( "\\s+" ).reverse.join ( " " ) );
alert( str.split( " " ).reverse.join ( " " ) );

Vì bạn muốn in trong một dòng mới

Enter a string: hello world
dlrow olleh
0

Đã trả lời ngày 4 tháng 12 năm 2015 lúc 8:17Dec 4, 2015 at 8:17

gurvinder372gurvinder372gurvinder372

Hãy thử mã này9 gold badges70 silver badges91 bronze badges

2

Enter a string: hello world
dlrow olleh
1

Hướng dẫn how do you reverse a sentence in javascript? - làm thế nào để bạn đảo ngược một câu trong javascript?

Vì bạn muốn in trong một dòng mới

65,4K9 Huy hiệu vàng70 Huy hiệu bạc91 Huy hiệu Đồng2 gold badges25 silver badges46 bronze badges

vardDec 4, 2015 at 8:18

4.0172 Huy hiệu vàng25 Huy hiệu bạc46 Huy hiệu đồngRamanlfc

Đã trả lời ngày 4 tháng 12 năm 2015 lúc 8:181 gold badge17 silver badges24 bronze badges

2

Ramanlfcramanlfc

OUTPUT:

Enter a string: hello world
dlrow olleh
3

8.1741 Huy hiệu vàng17 Huy hiệu bạc24 Huy hiệu đồng

Enter a string: hello world
dlrow olleh
2Dec 4, 2015 at 8:19

JSfiddleJacob George

Đã trả lời ngày 4 tháng 12 năm 2015 lúc 8:191 gold badge15 silver badges28 bronze badges

2

Làm thế nào để bạn đảo ngược văn bản trong javascript?

Sử dụng hàm lart () trong javascript để đảo ngược mảng các ký tự, tức là ['s', 'k', 'e', ​​'e', ​​'g', '', 'r', 'o', 'f', '', 's', 'k', 'e', ​​'e', ​​'g'] sử dụng hàm nối () trong javascript để tham gia các phần tử của một mảng thành một chuỗi. i.e. [ 's', 'k', 'e', 'e', 'G', ' ', 'r', 'o', 'f', ' ', 's', 'k', 'e', 'e', 'G' ] Use join() function in JavaScript to join the elements of an array into a string.

Làm thế nào để bạn đảo ngược các từ trong một câu?

Từ ngược trong một chuỗi đã cho ..
Xóa khoảng trống khỏi một chuỗi đã cho ..
Di chuyển không gian đến phía trước chuỗi trong một lần chuyển tiếp ..
Xóa thêm khoảng trống khỏi một chuỗi ..
Urlify một chuỗi đã cho (thay thế khoảng trắng bằng %20).
In tất cả các chuỗi có thể được thực hiện bằng cách đặt không gian ..
Đặt không gian giữa các từ bắt đầu bằng chữ in hoa ..

Làm thế nào để bạn đảo ngược một câu trong HTML?

Thẻ được sử dụng để ghi đè hướng văn bản hiện tại. tag is used to override the current text direction.

Làm thế nào để bạn đảo ngược một câu lệnh chuỗi?

Bằng cách sử dụng StringBuilder StringBuilder hoặc StringBuffer Class có phương thức trong chế độ Build Reversion () để đảo ngược các ký tự trong chuỗi.Phương pháp này thay thế chuỗi của các ký tự theo thứ tự ngược lại.Phương pháp đảo ngược là phương thức tĩnh có logic để đảo ngược một chuỗi trong Java.StringBuilder or StringBuffer class has an in-build method reverse() to reverse the characters in the string. This method replaces the sequence of the characters in reverse order. The reverse method is the static method that has the logic to reverse a string in Java.