Home > Backend Development > C++ > body text

Print the last character of each word in a string

WBOY
Release: 2023-09-17 09:37:02
forward
1161 people have browsed it

Print the last character of each word in a string

introduce

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 position of the word starts from 0.

  • Every word is associated with a different length.

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

  • By default, words are separated by space characters.

  • Every word contains at least one character.

In this article, we will develop a code that takes a string as input and displays the last character of each word in that string. Let us see the following example to understand this topic better -

Sample Example

Example 1−

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

For example, in the fourth word of this string, only one character appears, so this is the last character of the string.

In this post, we will develop a piece of code that uses the indexing operator to extract the last character of each word and then access the previous character individually.

Syntax

str.length()
Copy after login
The Chinese translation of

length()

is:

length()

In C, the length() method is used to count the number of characters in a string. It works in linear order of strings.

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 equal to the space character, the i-1th index character is displayed because it is the last character of the previous word.

Example

The following C code snippet accepts a sample string as input and counts 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);
}
Copy after login

Output

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

Conclusion

In C, in the sentence format of strings, all words are separated by space characters. Each word of the string consists of uppercase and lowercase characters. It is easy to extract these characters and operate on them using the corresponding index.

The above is the detailed content of Print the last character of each word in a string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template