An warware: cire harafin ƙarshe a cikin kirtani

A cikin C++, igiyoyi jerin haruffa ne waɗanda za a iya sarrafa su ta hanyoyi daban-daban, kamar ƙara, maye gurbin, ko cire haruffa. Ɗaya daga cikin buƙatun gama gari a cikin sarrafa kirtani shine cire harafin ƙarshe daga kirtani. Wannan aikin yana da sauƙi a cikin C++, inda ayyuka masu ƙarfi da ɗakunan karatu ke wanzu don sauƙaƙe aikinmu.

Kuna iya cimma wannan aikin ta amfani da kirtani :: substr da kirtani :: ayyuka masu tsayi da aka gina a cikin C ++. string :: substr shine daidaitaccen aikin ɗakin karatu a cikin C ++ wanda ke haifar da ƙananan igiyoyi na asali, kuma kirtani :: tsayi aiki ne da ke ba mu tsayin kirtani.

string str = "Sannu Duniya";
str = str.substr (0, str.tsawon ()-1);

A cikin lambar da ke sama, ana adana kirtan “Hello Duniya” a cikin madaidaicin 'str'. Yin amfani da str.substr (0, str.length() -1), muna umurtar C++ don ƙirƙirar ƙananan igiyoyin da ke farawa daga 0th index (H) har zuwa tsawon kirtani ban da ɗaya. Wannan yadda ya kamata ya kawar da halin ƙarshe na kirtani.

Bari mu karya daidai yadda wannan ke aiki.

Fahimtar std :: kirtani :: substr

Aikin ** substr()** wani ɓangare ne na ɗakin karatu na kirtani a C++. Yana karɓar sigogi guda biyu: fihirisar farawa da tsawon saƙar kirtani. Fihirisar farawa ta tushen 0 ne, ma'ana hali na farko yana a index 0. Wannan aikin yana haifar da igiyar ƙasa kuma ya dawo da shi.

A cikin lambar mu, mun wuce 0 a matsayin alamar farawa, wanda shine farkon kirtani. Tsawon kirtani shine str.size () - 1, wanda shine dukan tsawon kirtani, ban da hali na ƙarshe.

Layin `str = str.substr(0, str.length()-1);` yana maye gurbin asalin kirtani tare da sabuwar igiyar da aka samar, don haka yadda ya kamata ya cire harafin ƙarshe daga kirtani.

Matsayin std::string:: tsayi

Aikin ** tsawon()** na ɗakin karatu na std :: kirtani yana mayar da adadin haruffa a cikin kirtani. Anan, muna amfani da aikin tsayi don nemo fihirisar ƙarshen aikin substr.

Ana ƙididdige ma'anar ƙarshen a matsayin tsayin kirtani da aka cire 1 saboda alamar kirtani suna farawa daga 0. Don haka idan tsayin kirtani ya kasance 11, ma'aunin halayen ƙarshe zai zama 10.

Yin amfani da waɗannan ayyuka biyu masu ƙarfi na ɗakin karatu na kirtani na C++, za mu iya sarrafa manipulations na kirtani da kyau kamar cire hali na ƙarshe daga kowane kirtani da aka bayar.

Ka tuna, a cikin shirye-shirye, fahimtar darussan yana da mahimmanci, idan ba haka ba, kamar fahimtar ka'idar da ke bayanta. Yi amfani da wannan misalin, wasa tare da lambar, kuma gwada sauran ayyukan da ake samu a cikin ɗakin karatu na C++.

Ƙarin Bayanan kula akan Strings da C++

Zaɓuɓɓuka ɗaya ne daga cikin mahimman nau'ikan da ake amfani da su a cikin shirye-shirye. Ana amfani da su don wakiltar rubutu kuma sun ƙunshi jerin haruffa. Saboda haka, ana iya sarrafa su ta hanyoyi daban-daban don cimma sakamakon da ake so. A cikin C++, ana ɗaukar igiyoyi azaman abubuwa. Ta hanyar ɗaukar kirtani azaman abubuwa, za mu iya yin amfani da ƙarfin shirye-shirye masu dacewa da abu da sauƙaƙe tsarin magudi.

Lokacin da ake mu'amala da kirtani a cikin C++, muna da ayyuka da ɗakunan karatu da yawa a hannunmu. Wasu suna ba mu damar yin amfani da musanya da musanya kirtani don biyan bukatunmu daidai.

Lokacin warware matsalar ku, mun yi amfani da haɗin ayyuka don cire hali na ƙarshe daga kirtani. Wannan hanyar ita ce ɗayan hanyoyi marasa ƙima don sarrafa igiyoyi a cikin C++.

A ƙarshe, koyaushe ku tuna cewa shirye-shiryen shine game da zabar kayan aiki mai dacewa don aikin da ya dace. Duk da yake substr da tsayi sune zaɓuɓɓukan da suka dace a wannan yanayin, C ++ yana ba da ɗimbin sauran hanyoyin yin magudin kirtani, kowanne yana da nasa shari'o'in amfani da fa'idodi.

Shafi posts:

Leave a Comment