📜  反转字符串的中间单词

📅  最后修改于: 2021-05-31 18:50:04             🧑  作者: Mango

给定一个字符串str ,打印除第一个和最后一个单词以外的所有单词。

例子:

Input  : Hi how are you geeks 
Output  : Hi woh era uoy geeks

Input : I am fine
Output : I ma fine
  1. 打印第一个字。
  2. 对于剩余的中间单词,请在到达每个单词的末尾时将其反向打印。这将打印除最后一个单词以外的所有单词的倒序。
  3. 打印最后一个字。
C++
// CPP program to print reverse of all words
// except the corner words.
#include 
using namespace std;
 
void printReverse(string str)
{
    // Print first word
    int i = 0;
    for (i = 0; i < str.length() && str[i] != ' '; i++)
        cout << str[i];
 
    // Print middle words
    string word = "";
    for (; i < str.length(); i++) {
 
        if (str[i] != ' ')
            word += str[i];
        else {
            reverse(word.begin(), word.end());
            cout << word << " ";
            word = "";
        }
    }
 
    // Print last word
    cout << word << " ";
}
 
int main()
{
    string str = "Hi how are you geeks";
    printReverse(str);
    return 0;
}


Java
// Java program to print reverse of all words
// except the corner words.
 
public class GFG{
 
static void printReverse(String str)
    {
        // Print first word
        int i = 0;
        for (i = 0; i < str.length() && str.charAt(i) != ' '; i++)
            System.out.print(str.charAt(i)) ;
     
        // Print middle words
        String word = "";
         
         
        for (; i < str.length(); i++) {
     
            if (str.charAt(i) != ' ')
                word += str.charAt(i);
                 
            else {
                    System.out.print(new StringBuilder(word).
                                reverse().toString() + " ");
                    word = "";
            }
        }
     
        // Print last word
        System.out.print(word + " ");
    }
     
    public static void main(String []args)
    {
        String str = "Hi how are you geeks";
        printReverse(str);
    }
 
// This code is contributed by Ryuga
}


Python3
# Python3 program to print reverse of all words
# except the corner words.
def printReverse(s):
 
    #Taking all the words in a list
    l = [s for s in s.split(' ')]
 
    #printing the first word as it is
    print(l[0], end = ' ')
 
     
    for i in range(1, len(l)-1):
 
        #printing middle words reversed
        print(l[i][::-1], end = ' ')
 
    #printing the last word as it is
    print(l[len(l)-1])
 
s = "Hi how are you geeks"
printReverse(s)


C#
// C# program to print reverse of all words
// except the corner words.
using System;
using System.Text;
 
class GFG
{
 
    static void printReverse(String str)
    {
        // Print first word
        int i = 0;
        for (i = 0; i < str.Length && str[i] != ' '; i++)
            Console.Write(str[i]) ;
     
        // Print middle words
        String word = "";
         
         
        for (; i < str.Length; i++)
        {
     
            if (str[i] != ' ')
                word += str[i];
                 
            else
            {
                word = reverse(word);
                    Console.Write(new StringBuilder(word).ToString() + " ");
                    word = "";
            }
        }
     
        // Print last word
        Console.Write(word + " ");
    }
    static String reverse(String input)
    {
        char[] temparray = input.ToCharArray();
        int left, right = 0;
        right = temparray.Length - 1;
 
        for (left = 0; left < right; left++, right--)
        {
            // Swap values of left and right
            char temp = temparray[left];
            temparray[left] = temparray[right];
            temparray[right] = temp;
        }
        return String.Join("",temparray);
    }
     
    // Driver code
    public static void Main(String []args)
    {
        String str = "Hi how are you geeks";
        printReverse(str);
    }
}
 
// This code contributed by Rajput-Ji


PHP


Javascript


输出:
Hi woh era uoy geeks

时间复杂度: O(n)
辅助空间: O(L)其中L是字符串最长单词的长度。

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”