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
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()
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); }
Output
Input String : Programming at TutorialsPoint Last words of each word in a string : g t t
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

If when opening a file that needs to be printed, we will find that the table frame line has disappeared for some reason in the print preview. When encountering such a situation, we must deal with it in time. If this also appears in your print file If you have questions like this, then join the editor to learn the following course: What should I do if the frame line disappears when printing a table in Excel? 1. Open a file that needs to be printed, as shown in the figure below. 2. Select all required content areas, as shown in the figure below. 3. Right-click the mouse and select the "Format Cells" option, as shown in the figure below. 4. Click the “Border” option at the top of the window, as shown in the figure below. 5. Select the thin solid line pattern in the line style on the left, as shown in the figure below. 6. Select "Outer Border"

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

Sometimes when we use PPT, we often need to print it out. However, we all know that there are many pages in PPT. If we print them one by one, is it really a waste? Therefore, I have personally tested it and it is OK to put 6 PPT pages on one page and then print on both sides. No paper is wasted, and the layout content can be seen clearly. So, do you know how to print 6 double-sided PPT sheets on one page? Next, I will tell you how to set it up. If you are interested, take a look! Step details: 1. First, we find the PPT that needs to be printed on the computer, and then double-click to open it. Click the inverted triangle next to the button on the upper left side of the page, find the [File] button in the drop-down menu, and click it; then, click [Print] in the information that appears. 2. Click

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package
