交换角词和反转中间字符
编写一个Java程序以获取输入字符串并交换第一个和最后一个单词并反转中间单词。
例子:
Input : Hello World GFG Welcomes You
Output :You semocleW GFG dlroW Hello
方法:
- 首先我们取两个空字符串,第一个字符串取第一个单词,第二个字符串取最后一个单词
- 当我们迭代每个单词时,我们必须注意指向除最后一个单词之外的下一个单词的变量。
- 现在我们反转给定字符串中的左字符串。
- 在上述过程之后,我们首先打印左字符串的最后一个单词和反转,然后是第一个单词。
C++
// C++ Program to illustrate the solution
// of above problem
#include
using namespace std;
void print(string s)
{
// Taking an Empty String
string fst = "";
int i = 0;
for (i = 0; i < s.length();) {
// Iterating from starting index
// When we get space, loop terminates
while (s[i] != ' ') {
fst = fst + s[i];
i++;
}
// After getting one Word
break;
}
// Taking an Empty String
string last = "";
int j = 0;
for (j = s.length() - 1; j >= i;) {
// Iterating from last index
// When we get space, loop terminates
while (s[j] != ' ') {
last = s[j] + last;
j--;
}
// After getting one Word
break;
}
// Printing last word
cout<= i; m--) {
// Reversing the left characters
cout<
Java
// Java Program to illustrate the solution of above problem
public class ExchangeFirstLastReverseMiddle {
static void print(String s)
{
// Taking an Empty String
String fst = "";
int i = 0;
for (i = 0; i < s.length();) {
// Iterating from starting index
// When we get space, loop terminates
while (s.charAt(i) != ' ') {
fst = fst + s.charAt(i);
i++;
}
// After getting one Word
break;
}
// Taking an Empty String
String last = "";
int j = 0;
for (j = s.length() - 1; j >= i;) {
// Iterating from last index
// When we get space, loop terminates
while (s.charAt(j) != ' ') {
last = s.charAt(j) + last;
j--;
}
// After getting one Word
break;
}
// Printing last word
System.out.print(last);
for (int m = j; m >= i; m--) {
// Reversing the left characters
System.out.print(s.charAt(m));
}
// Printing the first word
System.out.println(fst);
}
public static void main(String[] args)
{
String s = "Hello World GFG Welcomes You";
print(s);
}
}
Python3
# Python3 Program to illustrate the solution
# of above problem
def Print(s) :
# Taking an Empty String
fst = ""
i = 0
while(i < len(s)) :
# Iterating from starting index
# When we get space, loop terminates
while (s[i] != ' ') :
fst = fst + s[i]
i += 1
# After getting one Word
break
# Taking an Empty String
last = ""
j = len(s) - 1
while(j >= i) :
# Iterating from last index
# When we get space, loop terminates
while (s[j] != ' ') :
last = s[j] + last
j -= 1
# After getting one Word
break
# Printing last word
print(last, end = "")
m = j
while m >= i :
# Reversing the left characters
print(s[m] , end = "")
m -= 1
# Printing the first word
print(fst, end = "")
# Driver code
s = "Hello World GFG Welcomes You"
Print(s)
# This code is contributed by divyeshrabadiya07
C#
// C# Program to illustrate the
//solution of above problem
using System;
class ExchangeFirstLastReverseMiddle
{
static void print(string s)
{
// Taking an Empty String
string fst = "";
int i = 0;
for (i = 0; i < s.Length;) {
// Iterating from starting index
// When we get space, loop terminates
while (s[i] != ' ')
{
fst = fst + s[i];
i++;
}
// After getting one Word
break;
}
// Taking an Empty String
string last = "";
int j = 0;
for (j = s.Length - 1; j >= i;) {
// Iterating from last index
// When we get space, loop terminates
while (s[j] != ' ') {
last = s[j] + last;
j--;
}
// After getting one Word
break;
}
// Printing last word
Console.Write(last);
for (int m = j; m >= i; m--) {
// Reversing the left characters
Console.Write(s[m]);
}
// Printing the first word
Console.Write(fst);
}
// Driver code
public static void Main()
{
string s = "Hello World GFG Welcomes You";
print(s);
}
}
//This code is contributed by vt_m
输出:
You semocleW GFG dlroW Hello