Home > Backend Development > C++ > body text

Print the first and last characters of each word in a string

WBOY
Release: 2023-08-29 11:25:06
forward
889 people have browsed it

Print the first and last characters of each word in a string

introduce

A C string is a contiguous storage of alphanumeric characters and special characters. Strings have the following properties −

  • Every C string is associated with a fixed length.

  • Character operations can be easily performed with the string characters.

  • A string consists of words separated by spaces.

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

Example Example

Example 1 −

str − “Key word of a string”</p><p>Output − Ky wd of aa sg
Copy after login

For example, in the fourth word of this string, only one character "a" appears, so these are the first and last characters of the string.

In this article, we will develop a code that extracts the last character of each word using the index operator. Here, we will develop a code that uses the index operator to extract the last character of each word and access the previous characters separately.

Syntax

str.length()
Copy after login
The translation of

length()

is:

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

  • Accepts an input string, str.

  • Use the length() method to calculate the length of the string and store it in the len variable.

  • Use for loop i to perform string iteration.

  • Extract and store specific characters of the string 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 equal to the last index of the string, len-1 characters are 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.

  • Since the first character of the next word, the i 1 index is also printed.

Example

The following C code snippet is used to input a sample string and count the first and last characters 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);
}
Copy after login

Output

Input String − I want to learn at TutorialsPoint
First and last words of each string − 
II wt to ln at Tt
Copy after login

Conclusion

In C strings, most character operations can be completed by traversing the string in one pass. The characters of a string can be easily accessed using their position in constant time. The index of a string starts at 0 and ends with the length of the string - 1.

The above is the detailed content of Print the first and last characters 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!