Dada la string str , la tarea es imprimir el último carácter de cada palabra en la string.
Ejemplos:
Entrada: str = «Geeks para Geeks»
Salida: srsEntrada: str = «aplicaciones informáticas»
Salida: rs
Enfoque: agregue un espacio, es decir, «» al final de la string dada, de modo que la última palabra de la string también vaya seguida de un espacio, como todas las demás palabras de la string. Ahora comience a recorrer la string carácter por carácter e imprima cada carácter seguido de un espacio.
A continuación se muestra la implementación del enfoque anterior:
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 = "Geeks for Geeks"; 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 = "Geeks for Geeks"; 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 = "Geeks for Geeks" 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 = "Geeks for Geeks"; 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 = "Geeks for Geeks"; printLastChar($str); // This code contributed by PrinciRaj1992 ?>
Javascript
<script> // JavaScript 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 (var 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 document.write(str[i - 1] + " "); } } // Driver code var str = "Geeks for Geeks"; printLastChar(str); </script>
Producción:
s r s
Publicación traducida automáticamente
Artículo escrito por Vaibhav_Arora y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA