本文概述
- C ++
- Java
- Python3
- C#
【打印字符串中每个单词的第一个和最后一个字符】例子:
Input: Geeks for geeks
Output: Gs fr gsInput: Computer applications
Output: Cr as
方法
- 从第一个字母到最后一个字母运行一个循环。
- 打印字符串的第一个和最后一个字母。
- 如果字符串中有空格, 则打印位于空格之前和之后的字符。
C ++
// CPP program to print
// the first and last character
// of each word in a String'
#include<
bits/stdc++.h>
using namespace std;
// Function to print the first
// and last character of each word.
void FirstAndLast(string str)
{
int i;
for (i = 0;
i <
str.length();
i++)
{
// If it is the first word
// of the string then print it.
if (i == 0)
cout<
<
str[i];
// If it is the last word of the string
// then also print it.
if (i == str.length() - 1)
cout<
<
str[i];
// If there is a space
// print the successor and predecessor
// to space.
if (str[i] == ' ' )
{
cout<
<
str[i-1]<
<
" " <
<
str[i+1];
}
}
}// Driver code
int main()
{
string str = "Geeks for Geeks" ;
FirstAndLast(str);
}// This code is contributed by
// Surendra_Gangwar
Java
// Java program to print
// the first and last character
// of each word in a Stringclass GFG {// Function to print the first
// and last character of each word.
static void FirstAndLast(String str)
{
int i;
for (i = 0 ;
i <
str.length();
i++) {// If it is the first word
// of the string then print it.
if (i == 0 )
System.out.print(str.charAt(i));
// If it is the last word of the string
// then also print it.
if (i == str.length() - 1 )
System.out.print(str.charAt(i));
// If there is a space
// print the successor and predecessor
// to space.
if (str.charAt(i) == ' ' ) {
System.out.print(str.charAt(i - 1 )
+ " "
+ str.charAt(i + 1 ));
}
}
}// Driver code
public static void main(String args[])
{
String str = "Geeks for Geeks" ;
FirstAndLast(str);
}
}
Python3
# Python3 program to print
# the first and last character
# of each word in a String'# Function to print the first
# and last character of each word.
def FirstAndLast(string):
for i in range ( len (string)):# If it is the first word
# of the string then print it.
if i = = 0 :
print (string[i], end = "")# If it is the last word of the string
# then also print it.
if i = = len (string) - 1 :
print (string[i], end = "")# If there is a space
# print the successor and predecessor
# to space.
if string[i] = = " " :
print (string[i - 1 ], string[i + 1 ], end = "")# Driver code
if __name__ = = "__main__" :
string = "Geeks for Geeks"
FirstAndLast(string)# This code is contributed by
# sanjeev2552
C#
// C# program to print
// the first and last character
// of each word in a String
using System;
class GFG
{ // Function to print the first
// and last character of each word.
static void FirstAndLast( string str)
{
int i;
for (i = 0;
i <
str.Length;
i++)
{ // If it is the first word
// of the string then print it.
if (i == 0)
Console.Write(str[i]);
// If it is the last word of the string
// then also print it.
if (i == str.Length - 1)
Console.Write(str[i]);
// If there is a space
// print the successor and predecessor
// to space.
if (str[i] == ' ' )
{
Console.Write(str[i - 1]
+ " "
+ str[i + 1]);
}
}
} // Driver code
public static void Main()
{
string str = "Geeks for Geeks" ;
FirstAndLast(str);
}
} // This code is contributed by Ryuga
输出如下:
Gs fr Gs
推荐阅读
- 迷宫中的老鼠问题(使用回溯算法解决)
- 使用最少的给定操作数将一个字符串转换为另一个字符串
- C++编程语言快速入门简介
- 字符串的字母数字缩写
- CSS如何实现元素对齐()
- 生产现场车间数字化可视化管理系统软件
- LVS+Keepalived 高可用群集(DR模式)
- #yyds干货盘点#Reactive访问Spring Data R2DBC
- LVS+Keepalived群集