搜尋
首頁後端開發C++列印字串中每個單字的最後一個字符

列印字串中每個單字的最後一個字符

介紹

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 −

  • 單字的位置從0開始。

  • Every word is associated with a different length.

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

  • 預設情況下,單字之間由空格字元分隔。

  • Every word contains at least one character.

在本文中,我們將開發一段程式碼,該程式碼以一個字串作為輸入,並顯示該字串中每個單字的最後一個字元。讓我們看下面的範例以更好地理解這個主題 -

Sample Example

範例1−

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

例如,在這個字串的第四個單字中,只有一個字元出現,因此這是該字串的最後一個字元。

在這篇文章中,我們將開發一段程式碼,使用索引運算子提取每個單字的最後一個字符,然後分別存取前一個字符。

Syntax

str.length()

length()

的中文翻譯為:

length()

在C 中,length()方法用於計算字串中的字元數。它按照字串的線性順序工作。

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.

  • #如果這個字符等於空格字符,則顯示第i-1個索引字符,因為它是前一個單字的最後一個字符。

Example

下面的C 程式碼片段用於接受一個範例字串作為輸入,並計算字串中每個單字的最後一個字元 -

//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);
}

輸出

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

Conclusion

在C 中,字串的句子格式中,所有單字都由空格字元分隔。字串的每個單字由大寫和小寫字元組成。使用相應的索引很容易提取這些字元並對其進行操作。

以上是列印字串中每個單字的最後一個字符的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
php怎么将16进制字符串转为数字php怎么将16进制字符串转为数字Oct 26, 2021 pm 06:36 PM

php将16进制字符串转为数字的方法:1、使用hexdec()函数,语法“hexdec(十六进制字符串)”;2、使用base_convert()函数,语法“bindec(十六进制字符串, 16, 10)”。

php怎么将字符串转换成小数php怎么将字符串转换成小数Mar 22, 2023 pm 03:22 PM

PHP 是一门功能强大的编程语言,广泛应用于 Web 开发领域。其中一个非常常见的情况是需要将字符串转换为小数。这在进行数据处理的时候非常有用。在本文中,我们将介绍如何在 PHP 中将字符串转换为小数。

golang怎么检测变量是否为字符串golang怎么检测变量是否为字符串Jan 06, 2023 pm 12:41 PM

检测变量是否为字符串的方法:1、利用​“%T”格式化标识,语法“fmt.Printf("variable count=%v is of type %T \n", count, count)”;2、利用reflect.TypeOf(),语法“reflect.TypeOf(变量)”;3、利用reflect.ValueOf().Kind()检测;4、使用类型断言,可以对类型进行分组。

php怎么将字符串转为布尔类型php怎么将字符串转为布尔类型Jul 01, 2021 pm 06:36 PM

转换方法:1、在转换变量前加上用括号括起来的目标类型“(bool)”或“(boolean)”;2、用boolval()函数,语法“boolval(字符串)”;3、用settype()函数,语法“settype(变量,"boolean")”。

go语言怎么删除字符串中的空格go语言怎么删除字符串中的空格Jan 17, 2023 pm 02:31 PM

删除方法:1、使用TrimSpace()函数去除字符串左右两边的空格,语法“strings.TrimSpace(str)”;2、使用Trim()函数去除字符串左右两边的空格,语法“strings.Trim(str, " ")”;3、使用Replace()函数去除字符串的全部空格,语法“strings.Replace(str, " ", "", -1)”。

php字符串函数学习:怎么去掉前面的字符php字符串函数学习:怎么去掉前面的字符Mar 20, 2023 pm 02:33 PM

在开发PHP应用程序时,有时我们需要去掉字符串前面的某些特定字符或者字符串。在这种情况下,我们需要使用一些PHP函数来实现这一目标。本文将介绍一些PHP函数,帮助您轻松地去掉字符串前面的字符或字符串。

php 字符串长度不一致怎么办php 字符串长度不一致怎么办Feb 07, 2023 am 09:58 AM

php字符串长度不一致的解决办法:1、通过mb_detect_encoding()函数查看字符串的编码方式;2、通过mb_strlen函数查看具体字符长度;3、使用正则表达式“preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str1, $matches);”剔除非中文字符即可。

php字符串部分乱码怎么办php字符串部分乱码怎么办Jan 20, 2023 am 10:18 AM

php字符串部分乱码的解决办法:1、使用“mb_substr(strip_tags($str),0,-1,'UTF-8');”截取字符串;2、使用“iconv("UTF-8","GB2312//IGNORE",$data)”转换字符集即可。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。