反转字符串中除第一个和最后一个字符之外的每个单词
给定由一个句子组成的字符串str ,任务是反转句子的每个单词,除了单词的第一个和最后一个字符。
例子:
Input: str = “geeks for geeks”
Output: gkees for gkees
Input: str = “this is a string”
Output: this is a snirtg
方法:使用 strtok() 将字符串分解为单词,现在为每个单词获取两个指针, i和j分别指向字符串的第二个和倒数第二个字符。交换这些字符,然后增加 i并减少 j 。在i < j时重复这些步骤。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to reverse the given word except
// the first and the last character
string reverseWord(string str)
{
int len = str.length();
// Pointer to the second character
// of the string
int i = 1;
// Pointer to the second last
// character of the string
int j = str.length() - 2;
while (i < j) {
// Swap str[i] and str[j]
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
return str;
}
// Function to reverse every word of the
// sentence except the first and the
// last character of the words
void reverseWords(char str[])
{
char* tok = strtok(str, " ");
// While there are words left
while (tok != NULL) {
// Print the reversed word
cout << reverseWord(tok) << " ";
// Get the next word
tok = strtok(NULL, " ");
}
}
// Driver code
int main()
{
char str[] = "geeks for geeks";
reverseWords(str);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to reverse the given word except
// the first and the last character
static String reverseWord(String str)
{
int len = str.length();
// Pointer to the second character
// of the string
int i = 1;
// Pointer to the second last
// character of the string
int j = str.length() - 2;
char[] strchar = str.toCharArray();
while (i < j)
{
// Swap str[i] and str[j]
char temp = strchar[i];
strchar[i] = strchar[j];
strchar[j] = temp;
i++;
j--;
}
str = new String(strchar);
return str;
}
// Function to reverse every word of the
// sentence except the first and the
// last character of the words
static void reverseWords(String str)
{
String[] tok = str.split("\\s");
// While there are words left
for(String w:tok)
{
// Print the reversed word
System.out.print(reverseWord(w) + " ");
}
}
// Driver code
public static void main (String[] args)
{
String str = "geeks for geeks";
reverseWords(str);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to reverse the given word except
# the first and the last character
def reverseWord(Str):
# len = len(Str)
# Pointer to the second character
# of the String
i = 1
# Pointer to the second last
# character of the String
j = len(Str) - 2
while (i < j):
# Swap Str[i] and Str[j]
temp = Str[i]
Str[i] = Str[j]
Str[j] = temp
i += 1
j -= 1
return "".join(Str)
# Function to reverse every word of the
# sentence except the first and the
# last character of the words
def reverseWords(Str):
Str = Str.split()
# While there are words left
for i in Str:
# Print the reversed word
j = [h for h in i]
print(reverseWord(j), end = " ")
# Driver code
Str= "geeks for geeks"
reverseWords(Str)
# This code is contributed by Mohit Kumar
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to reverse the given word except
// the first and the last character
static String reverseWord(String str)
{
int len = str.Length;
// Pointer to the second character
// of the string
int i = 1;
// Pointer to the second last
// character of the string
int j = str.Length - 2;
char[] strchar = str.ToCharArray();
while (i < j)
{
// Swap str[i] and str[j]
char temp = strchar[i];
strchar[i] = strchar[j];
strchar[j] = temp;
i++;
j--;
}
str = new String(strchar);
return str;
}
// Function to reverse every word of the
// sentence except the first and the
// last character of the words
static void reverseWords(String str)
{
String[] tok = str.Split(' ');
// While there are words left
foreach(String w in tok)
{
// Print the reversed word
Console.Write(reverseWord(w) + " ");
}
}
// Driver code
public static void Main (String[] args)
{
String str = "geeks for geeks";
reverseWords(str);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
gkees for gkees