Global Trend Radar
Dev.to US tech 2026-05-08 13:38

文中の単語を逆にする

原題: Reverse Words in a Sentence

元記事を開く →

分析結果

カテゴリ
宇宙
重要度
49
トレンドスコア
11
要約
文中の単語を逆にする方法について説明します。具体的には、文を空白で分割し、単語の順序を逆にして再結合する手法を紹介します。このプロセスにより、元の文の意味を保持しつつ、単語の並びを変更することができます。
キーワード
JavaScript Program: let sentence = " how are you " ; let end = sentence . length - 1 ; let word = "" ; let start ; for ( let i = end ; i >= 0 ; i -- ) { if ( sentence [ i ] === " " || i === 0 ) { let start ; if ( i === 0 ) { start = i ; } else { start = i + 1 ; } for ( let j = start ; j <= end ; j ++ ) { word = word + sentence [ j ]; } console . log ( word ); word = "" ; end = i - 1 ; } } output: Algorithm: Start Initialize a string sentence = "how are you" Set: end = length of sentence - 1 word = "" (empty string) Loop from end to start of the string (i = end → 0) For each character: Check if space OR i == 0 If condition is true: Determine start index: If i == 0 → start = i Else → start = i + 1 Extract the word: Loop from start → end Add each character to word Print the word Reset: word = "" end = i - 1 Continue loop until i = 0 Stop JavaScript Program: let sentence = " how are you " ; let end = sentence . length - 1 ; let word = "" ; let start ; for ( let i = end ; i >= 0 ; i -- ) { if ( sentence [ i ] === " " || i === 0 ) { let start ; if ( i === 0 ) { start = i ; } else { start = i + 1 ; } for ( let j = start ; j <= end ; j ++ ) { word = word + sentence [ j ]; } console . log ( word ); word = "" ; end = i - 1 ; } } output: Algorithm: Start Initialize a string sentence = "how are you" Set: end = length of sentence - 1 word = "" (empty string) Loop from end to start of the string (i = end → 0) For each character: Check if space OR i == 0 If condition is true: Determine start index: If i == 0 → start = i Else → start = i + 1 Extract the word: Loop from start → end Add each character to word Print the word Reset: word = "" end = i - 1 Continue loop until i = 0 Stop