Print last character of each word in a string


Introduction

C++ strings are essentially a combination of words used as a storage unit to contain alphanumeric data. The words in a string are associated with the following properties −

  • The word positions begin with 0.

  • Every word is associated with a different length.

  • The characters combine together to form words, which eventually form sentences.

  • By default, the words are separated by a space character.

  • Every word contains at least one character.

In this article, we are going to develop a code that takes as input a string, and displays the last character of each word in the string. Let us look at the following example to understand the topic better −

Sample Example

Example 1 −

str − “Key word of a string”
Output − y d f a g

For instance, in the fourth word of this string, only a single character occurs, therefore this is the last character of this string.

In this article, we will develop a code to extract the last character of each word using indexing operator ,and then accessing the characters at the subsequent previous characters respectively.

Syntax

str.length()

length()

The length() method in C++ is used to compute the number of characters in the string. It works in the linear order of the string time.

Algorithm

  • An input string, str is accepted.

  • The length of the string is computed using the length() method and stored in len variable.

  • An iteration of the string is performed, using the for loop i.

  • Each time the character at ith position is extracted, stored in variable ch

  • If this character is equivalent to the last index of the string, that is len-1, it is displayed.

  • If this character is equivalent to the space character, then the i-1th index character is displayed, since it is the last character of the previous word.

Example

The following C++ code snippet is used to take as input a sample string and compute the last character of each word in the string −

//including the required libraries
#include<bits/stdc++.h>
using namespace std;
//compute last characters of a string 
void  wordlastchar(string str) {
   // getting length of the string 
   int len = str.length();
   for (int i = 0; i <len ; i++) {
      char ch = str[i];
     
      //last word of the string
      if (i == len - 1)
         cout<<ch;
 
      //if a space is encountered, marks the start of new word 
      if (ch == ' ') {
         //print the previous character of the last word 
           
          char lst = str[i-1];
          cout<<lst<<" ";
       }
    }
}
 
//calling the method
int main() {
   //taking a sample string 
   string str = "Programming at TutorialsPoint";
   cout<<"Input String : "<< str <<"\n"; 
   //getfirstandlast characters
   cout<<"Last words of each word in a string : \n";
   wordlastchar(str);
}

Output

Input String : Programming at TutorialsPoint
Last words of each word in a string : 
g t t

Conclusion

In the sentence case of a string, followed in C++ all words are separated by space characters.Every word of a string is composed of both upper case and lower case characters. It is very easy to extract these characters using their corresponding indices and perform manipulations on it.

Updated on: 31-Jul-2023

605 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements