Jump to content

C++ String Sorunu


yow_sky
 Share

Recommended Posts

c++ İYİ BİLEN ARKADASLARA..  BİR STRİNG DİZİSİ İÇERSİNDE İSİM KAYITLI ATIYORUM mehmet_ali_alabora  burada bulanan ''_'' göre isim soy isim olarak nasıl ayırırız tek isimli oldumu sıkıntı yok ama iki tane isim oldumu sıkıntı oluyor lütfen yardımcı olun bana ? Kullandığım program visiual studio

Link to comment
Share on other sites

Herhangi bi kod parçası vermediğin için aşağıdaki örneği tamamen farazi yazdım, kendine göre düzenlersin;

 unsigned found = str.find_last_of('_');   //str şu anki stringin
 cout << " isim kısmı: " << str.substr(0,found) << '\n';
 cout << " soy isim  : " << str.substr(found+1) << '\n';

iki isimlilerde "_" silmen gerekebilir onun içinde replace metodunu kullanabilirsin.

Link to comment
Share on other sites

Mantık şu kardeşim eger okutmaya en sondan itibaren  başlarsak gördüğümüz ilk  " _ " sonrası isme denk gelir ikili isimler olsun tekli isimler olsun farketmez buna uygun bir kod üzerinde uğraşıyorum ama beceremedim bir türlü 

Link to comment
Share on other sites

Verdiğim kod zaten bu işe yarıyor. "str" değişkeninin içeriğinde "alt tireli isim" olduğunu varsaydım.Kodunu ona göre düzenlemen gerekiyor.

 

Edit: find_last_of metodu verdiğin parametreden bulunan son char ın poziyonunu verir.Parametre olarak "_" verdik, metod bize string içinde o değerin son bulunduğu yeri verecek.Buda soyisimden önceki "_" oluyor.Bulunan poziyona göre substr ile ayırma işlemi yapıyorsun.Acaba yazdığım şeyi hiç denedin mi?

 

Edit2: 

#include <string>
#include <iostream>

using namespace std;

int main(){
	string tnctr = "Mehmet_Ali_Yılmaz";
	unsigned found = tnctr.find_last_of('_');   //str şu anki stringin
	cout << "isim kısmı: " << tnctr.substr(0,found) << endl;
	cout << "soy isim  : " << tnctr.substr(found+1) << endl;
	return 0;
}

Çalışan örnek de ekledim.

Edited by MostWanted
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...