以相反的顺序打印字符串的单词
让一个字符串说“我是一个极客”。所以,输出应该是“GEEK A AM I”。这可以通过多种方式完成。解决方案之一在字符串中的反向单词中给出。
例子:
Input : I AM A GEEK
Output : GEEK A AM I
Input : GfG IS THE BEST
Output : BEST THE IS GfG
这可以通过使用“ %s 格式说明符”的属性以更简单的方式完成。
属性:%s 将获取所有值,直到它变为 NULL 即 '\0'。
示例:char String[] = “I AM A GEEK” 存储如下图所示:
方法:从最后一个字符开始遍历字符串,向第一个字符移动。遍历时,如果遇到空格字符,则在该位置放置一个 NULL 并在 NULL字符之后打印剩余的字符串。重复此操作,直到循环结束,当循环结束时,打印字符串,%s 将打印字符,直到遇到第一个 NULL字符。
让我们在图表的帮助下查看该方法:
step 1:从最后一个字符开始遍历,直到遇到空格字符。
步骤 2:在空格字符的位置放置一个 NULL字符并打印其后的字符串。
步骤3:最后,循环到达第一个字符时结束,所以打印剩余的字符,它将打印第一个NULL字符,因此将打印第一个单词。
C++
// C++ program to print reverse
// of words in a string.
#include
using namespace std;
string wordReverse(string str)
{
int i = str.length() - 1;
int start, end = i + 1;
string result = "";
while (i >= 0) {
if (str[i] == ' ') {
start = i + 1;
while (start != end)
result += str[start++];
result += ' ';
end = i;
}
i--;
}
start = 0;
while (start != end)
result += str[start++];
return result;
}
// Driver code
int main()
{
string str = "I AM A GEEK";
cout << wordReverse(str);
return 0;
}
// This code is contributed
// by Imam
C
// C program to print reverse of words in
// a string.
#include
#include
void printReverse(char str[])
{
int length = strlen(str);
// Traverse string from end
int i;
for (i = length - 1; i >= 0; i--) {
if (str[i] == ' ') {
// putting the NULL character at the
// position of space characters for
// next iteration.
str[i] = '\0';
// Start from next character
printf("%s ", &(str[i]) + 1);
}
}
// printing the last word
printf("%s", str);
}
// Driver code
int main()
{
char str[] = "I AM A GEEK";
printReverse(str);
return 0;
}
Java
// Java program to print reverse
// of words in a string.
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static String wordReverse(String str)
{
int i = str.length() - 1;
int start, end = i + 1;
String result = "";
while (i >= 0) {
if (str.charAt(i) == ' ') {
start = i + 1;
while (start != end)
result += str.charAt(start++);
result += ' ';
end = i;
}
i--;
}
start = 0;
while (start != end)
result += str.charAt(start++);
return result;
}
// Driver code
public static void main(String[] args)
{
String str = "I AM A GEEK";
System.out.print(wordReverse(str));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
Python3
# Python3 program to print reverse
# of words in a string.
def wordReverse(str):
i = len(str)-1
start = end = i+1
result = ''
while i >= 0:
if str[i] == ' ':
start = i+1
while start != end:
result += str[start]
start += 1
result += ' '
end = i
i -= 1
start = 0
while start != end:
result += str[start]
start += 1
return result
# Driver Code
str = 'I AM A GEEK'
print(wordReverse(str))
# This code is contributed
# by SamyuktaSHegde
C#
// C# program to print reverse
// of words in a string.
using System;
class GFG {
static String wordReverse(String str)
{
int i = str.Length - 1;
int start, end = i + 1;
String result = "";
while (i >= 0) {
if (str[i] == ' ') {
start = i + 1;
while (start != end)
result += str[start++];
result += ' ';
end = i;
}
i--;
}
start = 0;
while (start != end)
result += str[start++];
return result;
}
// Driver code
public static void Main()
{
String str = "I AM A GEEK";
Console.Write(wordReverse(str));
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
PHP
= 0)
{
if($str[$i] == ' ')
{
$start = $i + 1;
while($start != $end)
$result = $result . $str[$start++];
$result = $result . ' ';
$end = $i;
}
$i--;
}
$start = 0;
while($start != $end)
$result = $result . $str[$start++];
return $result;
}
// Driver code
$str = "I AM A GEEK";
echo wordReverse($str);
// This code is contributed by ita_c
?>
Javascript
C++
#include
#include
#include
using namespace std;
string reverse_words(string s)
{
int left = 0, i = 0, n = s.size();
while (s[i] == ' ')
i++;
left = i;
while (i < n)
{
if (i + 1 == n || s[i] == ' ')
{
int j = i - 1;
if (i + 1 == n)
j++;
while (left < j)
swap(s[left++], s[j--]);
left = i + 1;
}
if (s[left] == ' ' && i > left)
left = i;
i++;
}
//reverse(s.begin(), s.end());
return s;
}
int main()
{
string str = "I AM A GEEK";
str = reverse_words(str);
cout << str;
return 0;
// This code is contributed
// by Gatea David
}
Javascript
输出:
GEEK A AM I
时间复杂度: O(len(str))
辅助空间: O(len(str))
不使用任何额外空间:
遍历字符串并镜像字符串中的每个单词,然后在最后镜像整个字符串。
以下 C++ 代码可以处理多个连续空格。
C++
#include
#include
#include
using namespace std;
string reverse_words(string s)
{
int left = 0, i = 0, n = s.size();
while (s[i] == ' ')
i++;
left = i;
while (i < n)
{
if (i + 1 == n || s[i] == ' ')
{
int j = i - 1;
if (i + 1 == n)
j++;
while (left < j)
swap(s[left++], s[j--]);
left = i + 1;
}
if (s[left] == ' ' && i > left)
left = i;
i++;
}
//reverse(s.begin(), s.end());
return s;
}
int main()
{
string str = "I AM A GEEK";
str = reverse_words(str);
cout << str;
return 0;
// This code is contributed
// by Gatea David
}
Javascript
输出:
GEEK A AM I
时间复杂度: O(len(str))
辅助空间: O(1)