- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print first and last character of each word in a string
Introduction
A C++ string is contiguous storage of alphanumeric characters as well as special characters. A string has the following properties −
Every C++ string is associated with a fixed length.
Character operations can be easily performed with the string characters.
A string is composed of words, which are space separated.
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 − Ky wd of aa sg
For instance, in the fourth word of this string, only a single character “a” occurs, therefore this is the first and last character of this string.
In this article, we will develop a code to extract the last character of each word using indexing operator. Here, 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. The in-built length() method works in linear time.
Algorithm
An input string, str is accepted.
The length of the string is computed using the length() method, and stored in the len variable.
An iteration of the string is performed, using the for loop i.
The particular character of string is extracted and stored in the variable ch.
Each time the character at ith position is extracted
If this index is equivalent to the first index of the string, it is printed
If this index is equivalent to the last index of the string,then len-1 character, 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.
The i+1th index is also printed owing to the first character of the next word respectively.
Example
The following C++ code snippet is used to take as input a sample string and compute the first and last character of each word in the string −
//including the required libraries #include<bits/stdc++.h> using namespace std; //compute the first and last characters of a string void wordfirstandlastchar(string str) { // getting length of the string int len = str.length(); for (int i = 0; i <len ; i++) { char ch = str[i]; //print the first word of the string if (i == 0) cout<<ch; //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 //print the next character of the next word char lst = str[i-1]; char nxt = str[i+1]; cout<<lst<<" "<<nxt; } } } //calling the method int main() { //taking a sample string string str = "I want to learn at TutorialsPoint"; cout<<"Input String : "<< str <<"\n"; //getfirstandlast characters cout<<"First and last words of each string : \n"; wordfirstandlastchar(str); }
Output
Input String − I want to learn at TutorialsPoint First and last words of each string − II wt to ln at Tt
Conclusion
Most of the character manipulation operations in C++ strings can be performed using a single traversal of the string. The characters of the string can be easily accessed using their positions in constant time. The indexes of the string begin with 0 and end with the string length-1 value.