將字串中每個單詞的首字母和尾字母大寫


簡介

在本教程中,我們實現了一種方法,用於將輸入字串中每個單詞的首字母和尾字母大寫。透過迭代輸入字串 str,每個單詞的起始和結束字母都被大寫。我們使用兩種方法用 C++ 程式設計實現了此問題。讓我們從一些演示開始本教程。

演示 1

String = “coding world”

輸出

CodinG WorlD

在上述演示中,考慮輸入字串以及將字串每個單詞的起始和結束字元大寫後的結果為 CodinG WorlD。

演示 2

String = “`hello all”

輸出

hellO AlL

在上述演示中,輸入字串為“hello all”。將字串中每個單詞的起始和結束字元大寫後的結果為 hellO AlL。

C++ 庫函式

length():它是一個字串類庫函式,在 C++ 標準庫中定義。它以位元組為單位返回輸入字串的長度。字串的長度是字串中字元的總數。

語法

string_name.length();

isalpha():此內建庫函式在 <ctype> 標頭檔案中定義。它檢查字串字元是否為字母。當字串的字元為字母時,它返回一個整數值,否則返回零。

語法

isalpha(char);

toupper():此 C++ 庫函式在 <cctype> 標頭檔案中定義。它將小寫字元轉換為大寫。

語法

toupper(char);

演算法

  • 獲取一個輸入字串。

  • 獲取一個字串陣列。

  • 使用 for 迴圈迭代輸入字串中的所有字元。

  • 檢查並大寫字串中每個單詞的首字母和尾字母。

  • 如果首字母和尾字母是大寫,則移動到下一個字元。

  • 檢查空格和非字母字元。

  • 列印結果字串。

示例 1

這裡,我們實現了上述示例之一。函式 capitalizeString() 返回結果字串。在 for 迴圈中,字串 newString 用作輸入字串的等價物。for 迴圈迭代輸入字串的每個字母。

#include<bits/stdc++.h>
using namespace std;

string capitalizeString(string s)
{

	// Creating an string similar to input string
	string newStr = s;
	for (int x = 0; x < newStr.length(); x++)
	{
		int l = x;
		while (x < newStr.length() && newStr[x] != ' ')
			x++;

		// Checking if character is capital or not

		newStr[l] = (char)(newStr[l] >= 'a' && newStr[l] <= 'z'
						? ((int)newStr[l] - 32)
						: (int)newStr[l]);

		newStr[x - 1] = (char)(newStr[x - 1] >= 'a' && newStr[x - 1] <= 'z'
							? ((int)newStr[x - 1] - 32)
							: (int)newStr[x - 1]);
	}

	return newStr;
}


int main()
{
	string s = "hello tutorials point";
	//cout << s << "\n";
	cout << "Resulting string after capitalizing the first and last alphabet of each word is : "<<capitalizeString(s);
}

輸出

Resulting string after capitalizing the first and last alphabet of each word is : HellO TutorialS PoinT

示例 2

實現將字串中每個單詞的首字母和尾字母大寫的任務。使用者定義的函式 capitalizeString() 迭代字串的每個字元並檢查單詞的首字母和尾字母。到達它們時,它將字元大寫。大寫的單詞儲存在 result 變數中並返回結果。

#include <iostream>
#include <string>
#include <cctype> 
using namespace std;

string capitalizeString(string str)
{
   // Flag to track if the current character is the first character of a word
    bool newWord = true; 

    for (size_t x = 0; x < str.length(); ++x) 
    {
        if (isalpha(str[x]))
        {
            // Check if the character is alphabetic
            if (newWord) 
            {
              // Capitalize the first character of the word
                str[x] = toupper(str[x]); 
                newWord = false;
            }
            if (x + 1 == str.length() || !isalpha(str[x + 1])) 
            {
               // Capitalize the last character of the word
                str[x] = toupper(str[x]); 
                newWord = true;
            }
        }
        else 
        {
            newWord = true;
        }
    }
    return str;
}

int main() 
{
    string str = "hello coding world";

    string result = capitalizeString(str);
    cout << "Resulting string after capitalizing first and last character is:  " << result << endl;

    return 0;
}

輸出

Resulting string after capitalizing first and last character is: HellO CodinG WorlD

結論

我們已經到達本教程的結尾。在本教程中,我們學習了一種方法,用於將輸入字串中每個單詞的首字母和大寫。我們透過兩個 C++ 程式碼實現了該方法。使用 length()、isalpha() 和 toupper() 等不同的 C++ 庫函式來實現該問題。

更新於: 2023-08-18

410 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.