本文概述
- C ++
- Java
- Python3
- C#
- 的PHP
例子:
【算法设计(如何打印字符串中每个单词的最后一个字符())】输入:str ="lsbin in lsbin"方法:附加一个空格""在给定字符串的末尾, 以便字符串中的最后一个单词后面也跟一个空格, 就像字符串中的所有其他单词一样。现在开始逐个字符遍历字符串, 并打印每个字符, 后跟一个空格。
输出:n n n
输入:str ="computer applications"
输出:r s
下面是上述方法的实现:
C ++
// CPP implementation of the approach
#include<
bits/stdc++.h>
using namespace std;
// Function to print the last character
// of each word in the given string
void printLastChar(string str)
{// Now, last word is also followed by a space
str = str + " " ;
for ( int i = 1;
i <
str.length();
i++)
{// If current character is a space
if (str[i] == ' ' )// Then previous character must be
// the last character of some word
cout <
<
str[i - 1] <
<
" " ;
}
}// Driver code
int main()
{
string str = "lsbin in lsbin" ;
printLastChar(str);
}// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation of the approach
class GFG {// Function to print the last character
// of each word in the given string
static void printLastChar(String str)
{// Now, last word is also followed by a space
str = str + " " ;
for ( int i = 1 ;
i <
str.length();
i++) {// If current character is a space
if (str.charAt(i) == ' ' )// Then previous character must be
// the last character of some word
System.out.print(str.charAt(i - 1 ) + " " );
}
}// Driver code
public static void main(String s[])
{
String str = "lsbin in lsbin" ;
printLastChar(str);
}
}
Python3
# Function to print the last character
# of each word in the given string
def printLastChar(string):# Now, last word is also followed by a space
string = string + " "
for i in range ( len (string)):# If current character is a space
if string[i] = = ' ' :# Then previous character must be
# the last character of some word
print (string[i - 1 ], end = " " )# Driver code
string = "lsbin in lsbin"
printLastChar(string)# This code is contributed by Shrikant13
C#
// C# implementation of the approach
using System;
class GFG
{ // Function to print the last character
// of each word in the given string
static void printLastChar( string str)
{ // Now, last word is also followed by a space
str = str + " " ;
for ( int i = 1;
i <
str.Length;
i++)
{ // If current character is a space
if (str[i] == ' ' ) // Then previous character must be
// the last character of some word
Console.Write(str[i - 1] + " " );
}
} // Driver code
public static void Main()
{
string str = "lsbin in lsbin" ;
printLastChar(str);
}
} // This code is contributed by Ryuga
的PHP
<
?php
// PHP implementation of the approach// Function to print the last character
// of each word in the given string
function printLastChar( $str )
{
// Now, last word is also followed by a space
$str = $str . " " ;
for ( $i = 1;
$i <
strlen ( $str );
$i ++)
{
// If current character is a space
if (! strcmp ( $str [ $i ], ' ' ))// Then previous character must be
// the last character of some word
echo ( $str [ $i - 1] . " " );
}
}// Driver code
$str = "lsbin in lsbin" ;
printLastChar( $str );
// This code contributed by PrinciRaj1992
?>
输出如下:
n n n
推荐阅读
- 算法(如何查找矩阵中每一列的最大元素())
- 如何在Windows中为Python安装OpenCV()
- JavaScript性能问题和优化指南
- Android自定义XML属性
- Android手机图片适配问题
- android权限--android开发中的权限及含义(上)
- android 安卓 微信布局
- Android菜鸟成长记9 -- selector的用法
- Android四大组件——Service后台服务前台服务IntentService跨进程服务无障碍服务系统服务