Bir programcı olarak, muhtemelen bir diziyi tersine çevirmenizi gerektiren bir durumla karşı karşıya kaldınız. Bir diziyi tersine çevirmek, programcıların kodlamayı öğrenirken karşılaştığı en yaygın durumlardan biridir. Yerleşik işlevleri kullanarak veya ters işlevin kendi uygulamanızı yazarak bir dizeyi tersine çevirebilirsiniz.
Bu makalede, C++, Python ve JavaScript'te bir dizeyi tersine çevirmek için farklı yöntemler hakkında bilgi edineceksiniz.
C++'da Bir Dizeyi Tersine Çevirmenin Farklı Yöntemleri
Aşağıdaki yöntemleri kullanarak bir dizeyi C++'da tersine çevirebilirsiniz:
Yerleşik reverse() İşlevini Kullanarak C++'da Bir Dizeyi Tersine Çevirme
Yerleşik kullanarak bir dizeyi tersine çevirmek için C++ programı aşağıdadır. ters() işlev:
// C++ implementation to reverse a string
// using inbuilt function: reverse()
#include
using namespace std;
// Driver Code
int main()
{
string str1 = 'MUO';
string str2 = 'Welcome to MUO';
string str3 = 'She sells seashells by the seashore';
cout << 'Input string:' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
reverse(str3.begin(), str3.end());
cout << 'Reversed string: ' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
C++'da Karakterleri Değiştirerek Bir Dizeyi Tersine Çevirin
Aşağıda, karakterleri değiştirerek bir dizeyi tersine çeviren C++ programı verilmiştir:
// C++ implementation to reverse a string
// by swapping characters
#include
using namespace std;
// Own implementation of a function to reverse a string
void reverseString(string& str)
{
int size = str.size();
for(int i=0, j=size-1; i {
swap(str[i], str[j]);
}
}
// Driver Code
int main()
{
string str1 = 'MUO';
string str2 = 'Welcome to MUO';
string str3 = 'She sells seashells by the seashore';
cout << 'Input string:' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
reverseString(str1);
reverseString(str2);
reverseString(str3);
cout << 'Reversed string: ' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Bir Yapıcı ile Ters Yineleyicileri Kullanarak C++'da Bir Dizeyi Tersine Çevirme
Bir yapıcı ile ters yineleyiciler kullanarak bir dizeyi tersine çevirmek için C++ programı aşağıdadır:
// C++ implementation to reverse a string
// using constructor
#include
using namespace std;
int main()
{
string str1 = 'MUO';
string str2 = 'Welcome to MUO';
string str3 = 'She sells seashells by the seashore';
cout << 'Input string:' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
// Using reverse iterators to reverse a string
string reversedStr1 = string(str1.rbegin(), str1.rend());
string reversedStr2 = string(str2.rbegin(), str2.rend());
string reversedStr3 = string(str3.rbegin(), str3.rend());
cout << 'Reversed string: ' << endl;
cout << reversedStr1 << endl;
cout << reversedStr2 << endl;
cout << reversedStr3 << endl;
return 0;
}
Çıktı:
başlat menüsü ve arama çalışmıyor
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Geçici Bir Dize Kullanarak C++'da Bir Dizeyi Tersine Çevirme
Geçici bir dize kullanarak bir dizeyi tersine çevirmek için C++ programı aşağıdadır:
// C++ implementation to reverse a string
// using a temporary string
#include
using namespace std;
// Function to reverse a string using a temporary string
string reverseString(string str)
{
int size = str.size();
string tempStr;
for(int i=size-1; i>=0; i--)
{
tempStr.push_back(str[i]);
}
return tempStr;
}
// Driver Code
int main()
{
string str1 = 'MUO';
string str2 = 'Welcome to MUO';
string str3 = 'She sells seashells by the seashore';
cout << 'Input string:' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
cout << 'Reversed string: ' << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
İlgili: Bir Dizedeki Ünlüler, Ünsüzler, Rakamlar ve Özel Karakterler Nasıl Bulunur?
Python'da Bir Dizeyi Tersine Çevirmenin Farklı Yöntemleri
Aşağıdaki yöntemleri kullanarak Python'da bir dizeyi tersine çevirebilirsiniz:
Genişletilmiş Dilim Sözdizimini Kullanarak Python'da Bir Dizeyi Tersine Çevirme
Aşağıda, genişletilmiş bir dilim sözdizimi kullanarak bir dizeyi tersine çevirmek için Python programı verilmiştir:
# Python implementation to reverse a string
# using extended slice syntax
def reverseString(str):
return str[::-1]
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
print('Input string:')
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print('Reversed string:')
print(str1)
print(str2)
print(str3)
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Özyinelemeyi Kullanarak Python'da Bir Dizeyi Ters Çevirme
Özyineleme kullanarak bir dizeyi tersine çevirmek için Python programı aşağıdadır:
İlgili: Özyineleme Nedir ve Nasıl Kullanırsınız?
# Python implementation to reverse a string
# using recursion
def reverseString(str):
if len(str) == 0:
return str
else:
return reverseString(str[1:]) + str[0]
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
print('Input string:')
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print('Reversed string:')
print(str1)
print(str2)
print(str3)
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Yerleşik reversed() Yöntemini Kullanarak Python'da Bir Dizeyi Tersine Çevirin
Yerleşik kullanarak bir dizeyi tersine çevirmek için Python programı aşağıdadır. ters() yöntem:
# Python implementation to reverse a string
# using reversed method()
def reverseString(str):
str = ''.join(reversed(str))
return str
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
print('Input string:')
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print('Reversed string:')
print(str1)
print(str2)
print(str3)
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Geçici Bir Dize Kullanarak Python'da Bir Dizeyi Tersine Çevirme
Geçici bir dize kullanarak bir dizeyi tersine çevirmek için Python programı aşağıdadır:
# Python implementation to reverse a string
# using a temporary string
def reverseString(str):
tempStr = ''
for s in str:
tempStr = s + tempStr
return tempStr
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
print('Input string:')
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print('Reversed string:')
print(str1)
print(str2)
print(str3)
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
JavaScript'te Bir Dizeyi Tersine Çevirmenin Farklı Yöntemleri
Aşağıdaki yöntemleri kullanarak JavaScript'te bir dizeyi tersine çevirebilirsiniz:
İlgili: JavaScript ile İlk React Uygulamanızı Nasıl Oluşturursunuz
Özyinelemeyi Kullanarak JavaScript'te Bir Dizeyi Tersine Çevirme
Özyinelemeyi kullanarak bir dizeyi tersine çevirmek için JavaScript programı aşağıdadır:
// JavScript implementation to reverse a string
// using recursion
function reverseString(str) {
if (str === '') {
return '';
} else {
return reverseString(str.substr(1)) + str.charAt(0);
}
}
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
document.write('Input string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write('Reversed string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Yerleşik Yöntemleri Kullanarak JavaScript'te Bir Dizeyi Tersine Çevirme
Yerleşik yöntemleri kullanarak bir dizeyi tersine çevirmek için JavaScript programı aşağıdadır:
// JavaScript implementation to reverse a string
// using inbuilt methods
function reverseString(str) {
return str.split('').reverse().join('');
}
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
document.write('Input string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write('Reversed string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Geçici Bir Dize Kullanarak JavaScript'te Bir Dizeyi Tersine Çevirme
Geçici bir dize kullanarak bir dizeyi tersine çevirmek için JavaScript programı aşağıdadır:
// JavScript implementation to reverse a string
// using a temporary string
function reverseString(str) {
var size = str.length;
tempStr = '';
for(let i=size-1; i>=0; i--)
{
tempStr += str[i];
}
return tempStr;
}
str1 = 'MUO';
str2 = 'Welcome to MUO';
str3 = 'She sells seashells by the seashore';
document.write('Input string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write('Reversed string:
');
document.write(str1 + '
');
document.write(str2 + '
');
document.write(str3 + '
');
Çıktı:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Dize Manipülasyonunu Öğrenin
Diziyle ilgili mülakat problemlerini çözmek için bir diziyi nasıl manipüle edeceğinizi bilmelisiniz. Bir dizeyi C++, Python, JavaScript, Java, C vb. herhangi bir programlama dilinde değiştirebilirsiniz.
Python, bir dizeyi işlemek için anlaşılması en kolay sözdizimini sağlar. Dizeyi işlemek size zor geliyorsa, Python'a bir şans verin; aldatıcı derecede basittir.
Paylaş Paylaş Cıvıldamak E-posta Python'u Öğrenmek? İşte Dizeleri Nasıl Manipüle EdeceğinizPython'da dizeleri kullanmak ve işlemek zor görünebilir, ancak aldatıcı bir şekilde basittir.
Sonrakini Oku İlgili konular- Programlama
- JavaScript
- piton
- Kodlama Eğitimleri
Yuvraj, Hindistan Delhi Üniversitesi'nde Bilgisayar Bilimleri lisans öğrencisidir. Full Stack Web Geliştirme konusunda tutkulu. Yazmadığı zamanlarda farklı teknolojilerin derinliğini keşfediyor.
lg tablet dokunmatik ekran çalışmıyorYuvraj Chandra'dan Daha Fazla
Haber bültenimize abone ol
Teknik ipuçları, incelemeler, ücretsiz e-kitaplar ve özel fırsatlar için bültenimize katılın!
Abone olmak için buraya tıklayın