Cách tách nguyên âm và phụ âm trong chuỗi trong C++

Làm ơn đăng nhập để tiếp tục

Đăng nhập

Bài 18 trên 39 • 1 lượt ủng hộ • 8. 45 phút

Trong video này chúng ta sẽ nghiên cứu về hai chương trình thao tác với chuỗi

Crack GATE & ESE với Unacademy

Nhận đăng ký và truy cập các khóa học trực tiếp và được ghi lại không giới hạn từ các nhà giáo dục tốt nhất của Ấn Độ
Tìm hiểu thêm

Chương trình C để in các nguyên âm trong một chuỗi và vị trí của chúng. Chúng ta cần tìm tất cả các nguyên âm (chữ thường và chữ hoa) trong một chuỗi đã cho và vị trí của nó

Ví dụ,

Đầu vào. Chào thế giới

đầu ra

Nguyên âm. điện tử tại địa điểm. 1
Nguyên âm. o tại địa điểm. 4
Nguyên âm. o tại địa điểm. 7

Giải pháp tìm nguyên âm trong Chuỗi

Giải pháp rất đơn giản, chúng ta có thể lưu chuỗi vào một mảng char. Truy xuất từng ký tự bằng vòng lặp for và so sánh chúng với tất cả các nguyên âm tôi. e. a, e, i, o, u, A, E, I, O, U. Nếu ký tự khớp, chúng ta có thể in định vị và ký tự

GHI CHÚ

Để đọc chuỗi đầu vào e. g. Hello World, trong hàm scanf, chúng ta cần sử dụng biểu thức scanf(“%[^\n]s”, str); . Nó sẽ đọc chuỗi bao gồm cả khoảng trắng. Nếu chúng ta sử dụng đơn giản như scanf(“%s”, str); . g. trong thế giới hello, chỉ có hello sẽ được đọc bởi scanf

Cho một chuỗi str. Nhiệm vụ là sửa đổi chuỗi bằng cách hoán đổi hai ký tự liền kề nếu cả hai đều là nguyên âm hoặc cả hai đều là phụ âm

ví dụ.  

Đầu vào. str = “geeksforgeeks” 
Đầu ra. geesfkogreesk 
Các chữ cái 'e' và 'e' trong geeksforgeeks là các nguyên âm nên chúng được hoán đổi để chuỗi trở thành geeksforgeeks.
Các chữ cái 'k' và 's' trong geeksforgeeks là phụ âm nên chúng được hoán đổi để chuỗi trở thành geeksforgeeks.
Các chữ cái 'k' và 'f' trong geekkforgeeks là phụ âm nên chúng được hoán đổi để chuỗi trở thành geesfkorgeeks.
Các chữ cái 'r' và 'g' trong geesfkorgeeks là phụ âm nên chúng được hoán đổi để chuỗi trở thành geesfkorgeeks.
Các chữ cái 'e' và 'e' trong geekfogreeks là các nguyên âm nên chúng được đổi chỗ để chuỗi trở thành geekfogreeks.
Các chữ cái 'k' và 's' trong geekfogreesk là các nguyên âm nên chúng được hoán đổi để chuỗi trở thành geekfogreesk.

Đầu vào. str = “gefeg” 
Đầu ra. gefeg 
Không có nguyên âm hoặc phụ âm tiếp diễn.

Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp

Cách tiếp cận.  

  • Duyệt qua các ký tự trong chuỗi
  • Xem xét ký tự hiện tại và ký tự tiếp theo
  • Nếu cả hai ký tự là phụ âm hoặc cả hai ký tự là nguyên âm
  • Sau đó trao đổi các ký tự
  • Khác tiếp tục quá trình cho đến khi kết thúc chuỗi

Dưới đây là việc thực hiện các phương pháp trên.  

C++




// C++ implementation of the above approach

#include

using namespace std;

 

// Function to check if a character is a vowel

bool isVowel(char // C++ implementation of the above approach0

// C++ implementation of the above approach1

// C++ implementation of the above approach2// C++ implementation of the above approach3// C++ implementation of the above approach4// C++ implementation of the above approach5

// C++ implementation of the above approach2// C++ implementation of the above approach7 // C++ implementation of the above approach8// C++ implementation of the above approach9 #include 0#include 1 #include 0#include 3

#include 4#include 0#include 6 #include 0#include 8#include 9

using0using1 using2using3

// C++ implementation of the above approach2using1 using6using3

using8

 

using9

namespace0

namespace1

// C++ implementation of the above approach1

// C++ implementation of the above approach2____34

// C++ implementation of the above approach2namespace6 namespace7namespace8 namespace9

 

using0std;1

using0____43

using0// C++ implementation of the above approach7 std;6

std;7std;8

 

std;7// Function to check if a character is a vowel0

std;7// Function to check if a character is a vowel2

// C++ implementation of the above approach2using8

 

// C++ implementation of the above approach2using1 // Function to check if a character is a vowel7

using8

 

// Function to check if a character is a vowel9

namespace8 bool1

// C++ implementation of the above approach1

// C++ implementation of the above approach2bool4bool5using3

 

// C++ implementation of the above approach2bool8

// C++ implementation of the above approach2using1 isVowel(1

using8

Java




isVowel(3

isVowel(4 isVowel(5

// C++ implementation of the above approach1

 

// C++ implementation of the above approach2isVowel(8

// C++ implementation of the above approach2char0

// C++ implementation of the above approach2char2 char3 isVowel(char // C++ implementation of the above approach0

// C++ implementation of the above approach2// C++ implementation of the above approach1

using0// C++ implementation of the above approach00

using0// C++ implementation of the above approach7 // C++ implementation of the above approach8// C++ implementation of the above approach9 #include 0#include 1 #include 0#include 3

// C++ implementation of the above approach09#include 0#include 6 #include 0#include 8#include 9

using0// C++ implementation of the above approach1

std;7using1 using2using3

using0using8

using0using1 using6____23

// C++ implementation of the above approach2using8

 

// C++ implementation of the above approach2using9

// C++ implementation of the above approach2namespace0

// C++ implementation of the above approach2____82 // C++ implementation of the above approach35____8 // C++ implementation of the above approach37

// C++ implementation of the above approach2// C++ implementation of the above approach1

using0

using0// C++ implementation of the above approach42

using0// C++ implementation of the above approach44

using0namespace6 namespace7namespace8 // C++ implementation of the above approach49// C++ implementation of the above approach50// C++ implementation of the above approach51// C++ implementation of the above approach52// C++ implementation of the above approach53

using0// C++ implementation of the above approach1

std;7char // C++ implementation of the above approach3// C++ implementation of the above approach50using3

std;7

std;7std;1

std;7std;3

std;7// C++ implementation of the above approach7 // C++ implementation of the above approach68// C++ implementation of the above approach52// C++ implementation of the above approach70

// C++ implementation of the above approach71// C++ implementation of the above approach72____052// C++ implementation of the above approach74

std;7// C++ implementation of the above approach1

// C++ implementation of the above approach71

// C++ implementation of the above approach71// Function to check if a character is a vowel0

_______071____081

_______071____083____052// C++ implementation of the above approach85

_______071____087____052// C++ implementation of the above approach89

std;7using8

using0using8

using0using1 // C++ implementation of the above approach96

// C++ implementation of the above approach2using8

 

// C++ implementation of the above approach2____59

________ 02 ________ 102 ________ 82 ________ 104 ________ 105

// C++ implementation of the above approach2// C++ implementation of the above approach1

using0#include 09____65using3

using0____113

// C++ implementation of the above approach2using8

using8

 

#include 17

Trăn 3




#include 18

 

#include 19

#include 20 #include 21

// C++ implementation of the above approach2____123#include 24 #include 25

// C++ implementation of the above approach2// C++ implementation of the above approach7 #include 28#include 24#include 24 // C++ implementation of the above approach9 #include 32 #include 23#include 24#include 24 #include 1 #include 32 #include 23#include 24#include 24 #include 3

#include 4#include 32 #include 23_______124#include 24 #include 6 #include 32 #include 23#include 24#include 24 #include 8#include 53

using0using1 #include 56using3

// C++ implementation of the above approach2using1 #include 60using3

 

#include 62

#include 63

#include 20 #include 65

// C++ implementation of the above approach2

// C++ implementation of the above approach2#include 68

// C++ implementation of the above approach2namespace6 #include 71#include 72 #include 73namespace7#include 75#include 76#include 77 // C++ implementation of the above approach52#include 53

 

using0#include 81

using0____183

using0// C++ implementation of the above approach7 #include 86#include 87 #include 88#include 89 // C++ implementation of the above approach52// C++ implementation of the above approach70#include 32

using0______37#include 95#include 96#include 87 #include 95#include 99#include 89 // C++ implementation of the above approach52using02

 

std;7using04

std;7using06

using07using08#include 89 // C++ implementation of the above approach52using11#include 24 using13#include 89 // C++ implementation of the above approach52using16

using17using18

// C++ implementation of the above approach2

// C++ implementation of the above approach2____221____124 using23

// C++ implementation of the above approach2using1 using26

 

using27

// C++ implementation of the above approach7 using29____124#include 24 using32 using33

// C++ implementation of the above approach2

// C++ implementation of the above approach2______221____124 bool5using3

 

// C++ implementation of the above approach2____241using42using43using44

// C++ implementation of the above approach2

using46

C#




using47

using using49

 

isVowel(4 isVowel(5

// C++ implementation of the above approach1

 

// C++ implementation of the above approach2isVowel(8

// C++ implementation of the above approach2char0

// C++ implementation of the above approach2char2 bool isVowel(char // C++ implementation of the above approach0

// C++ implementation of the above approach2// C++ implementation of the above approach1

using0____03____8using68

using0// C++ implementation of the above approach7 // C++ implementation of the above approach8// C++ implementation of the above approach9 #include 0#include 1 #include 0#include 3

// C++ implementation of the above approach09#include 0#include 6 #include 0#include 8#include 9

using0// C++ implementation of the above approach1

std;7using1 using2using3

using0using8

using0using1 using6____23

// C++ implementation of the above approach2using8

 

// C++ implementation of the above approach2using9

// C++ implementation of the above approach2namespace0

// C++ implementation of the above approach2char2 // C++ implementation of the above approach35char namespace05

// C++ implementation of the above approach2// C++ implementation of the above approach1

using0

using0// C++ implementation of the above approach42

using0// C++ implementation of the above approach44

using0namespace6 namespace7namespace8 namespace17

using0// C++ implementation of the above approach1

std;7char namespace22charnamespace24

std;7

std;7std;1

std;7std;3

std;7// C++ implementation of the above approach7 std;6

// C++ implementation of the above approach71std;8

std;7// C++ implementation of the above approach1

// C++ implementation of the above approach71

// C++ implementation of the above approach71// Function to check if a character is a vowel0

_______071____081

_______071____343

_______071____345

std;7using8

using0using8

using0using1 namespace52namespace53namespace54

// C++ implementation of the above approach2using8

 

// C++ implementation of the above approach2____59

________ 02 ________ 102 ________ 82 ________ 104 ________ 363

// C++ implementation of the above approach2// C++ implementation of the above approach1

using0#include 09____65using3

using0____371

// C++ implementation of the above approach2using8

using8

 

namespace75

PHP




namespace76

 

namespace77

 

// Function to check if a character is a vowel

namespace79 isVowel(____381#include 9

// C++ implementation of the above approach1

// C++ implementation of the above approach2______381 #include 24____387namespace7namespace81namespace90

// C++ implementation of the above approach2// C++ implementation of the above approach7 namespace7namespace81 namespace95// C++ implementation of the above approach9 namespace97namespace81 namespace95#include 1 namespace97namespace81 namespace95#include 3

// C++ implementation of the above approach71namespace97namespace81 namespace95#include 6 namespace97namespace81 namespace95#include 8#include 9

using0using1 std;17

// C++ implementation of the above approach2using1 std;20

using8

 

using9

namespace0

namespace79 std;25std;26#include 9

// C++ implementation of the above approach1

// C++ implementation of the above approach2____34

// C++ implementation of the above approach2namespace6 namespace7std;34 std;35std;34 std;37std;38namespace7std;26std;41std;34std;43

 

using0std;1

using0____43

using0// C++ implementation of the above approach7 std;50std;26std;52std;34std;54std;26std;52std;34 std;58

std;7std;60______426std;52std;34std;64std;26std;52std;34 std;68

using0// C++ implementation of the above approach1

std;7// Function to check if a character is a vowel0

std;7std;74 #include 24____426std;52std;34// C++ implementation of the above approach85

_______47____426____452____434____484std;26____452std;34 std;88

_______47____426____452____434std;93std;74using3

using0using8

// C++ implementation of the above approach2using8

 

// C++ implementation of the above approach2using1 std;26using3

using8

 

// C++ implementation of the above approach2____59

 

// C++ implementation of the above approach2______426 #include 24____65using3

 

// C++ implementation of the above approach2____513 std;25____426namespace90

// C++ implementation of the above approach2using1 isVowel(1

 

// Function to check if a character is a vowel20

// Function to check if a character is a vowel21

Javascript




// Function to check if a character is a vowel22

_______523____524

// Function to check if a character is a vowel23isVowel(8

// Function to check if a character is a vowel23char0

// Function to check if a character is a vowel23namespace79 // Function to check if a character is a vowel31

using0// Function to check if a character is a vowel33

using0// C++ implementation of the above approach7 // Function to check if a character is a vowel36// Function to check if a character is a vowel37 // Function to check if a character is a vowel38// Function to check if a character is a vowel39 // Function to check if a character is a vowel38// Function to check if a character is a vowel41 // Function to check if a character is a vowel38// Function to check if a character is a vowel43 // Function to check if a character is a vowel38// Function to check if a character is a vowel45// Function to check if a character is a vowel46

Tại sao nguyên âm và phụ âm được tách ra?

Sự tách biệt giữa phụ âm và nguyên âm tồn tại vì nguyên âm là phần to hơn của lời nói (vì miệng của chúng ta mở rộng hơn khi nói một nguyên âm hơn .

Thứ tự đúng của các bước để tính số lượng nguyên âm và phụ âm trong một chuỗi là gì?

Thuật toán .
Xác định một chuỗi
Chuyển đổi chuỗi thành chữ thường để có thể giảm so sánh. .
Nếu bất kỳ ký tự nào trong chuỗi khớp với nguyên âm (a, e, i, o, u ) thì hãy tăng vcount lên 1
Nếu bất kỳ ký tự nào nằm giữa 'a' và 'z' ngoại trừ các nguyên âm, thì hãy tăng số đếm cho ccount lên 1
In cả hai số đếm