Kako programiranjem pretvoriti znakove niza u suprotni slučaj

Kako programiranjem pretvoriti znakove niza u suprotni slučaj

Niz je niz znakova. U ovom ćete članku naučiti kako pretvoriti znakove niza u suprotna slova. Naučit ćete i kako riješiti ovaj problem pomoću najpopularnijih programskih jezika kao što su C ++, Python, C i JavaScript.





Izjava o problemu

Daje vam se niz. Morate pretvoriti sve znakove ovog niza u suprotne slučajeve.





Primjer 1 : Let str = 'Dobrodošli u MUO'





ikona napajanja nedostaje na programskoj traci Windows 10

Niz nakon pretvaranja svih znakova u suprotne slučajeve = 'DOBRODOŠLI NA muo'

Dakle, izlaz je 'WELCOME TO muo'.



Primjer 2 : Let str = 'Fuzzy Wuzzy je bio medvjed. Fuzzy Wuzzy nije imao kose. '

Niz nakon pretvaranja svih znakova u suprotne slučajeve = 'FUZZY WUZZY WAS BED MEDVED. FUZZY WUZZY NEMA KOSE. '





Dakle, izlaz je 'FUZZY WUZZY WAS BED MEDVED. FUZZY WUZZY NEMA KOSE. '.

Primjer 3 : Let str = 'Tom je Timu bacio tri sličice'





Niz nakon pretvaranja svih znakova u suprotne slučajeve = 'tOM THREW TIM THREE THUMBTACKS'

Dakle, izlaz je 'tOM THREW THIM THREE THUMBTACKS'.

Povezano: Kako provjeriti jesu li dva niza međusobno anagrami

C ++ program za pretvaranje znakova niza u suprotne slučajeve

Ispod je program C ++ za pretvaranje znakova niza u suprotne slučajeve:

// C++ program to convert characters of string to opposite case
#include
using namespace std;
string convertString(string& str)
{
int length = str.length();
for (int i = 0; i {
// If the character is in lowercase,
// convert it to uppercase
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
// If the character is in uppercase,
// convert it to lowercase
else if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
}
return str;
}
int main()
{
string str1 = 'Welcome to MUO';
cout << 'Original String 1:' << endl;
cout << str1 << endl;
str1 = convertString(str1);
cout << 'Converted String 1:' << endl;
cout << str1 << endl;
string str2 = 'Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.';
cout << 'Original String 2:' << endl;
cout << str2 << endl;
str2 = convertString(str2);
cout << 'Converted String 2:' << endl;
cout << str2 << endl;
string str3 = 'Tom threw Tim three thumbtacks';
cout << 'Original String 3:' << endl;
cout << str3 << endl;
str3 = convertString(str3);
cout << 'Converted String 3:' << endl;
cout << str3 << endl;
return 0;
}

Izlaz:

Original String 1:
Welcome to MUO
Converted String 1:
wELCOME TO muo
Original String 2:
Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.
Converted String 2:
fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.
Original String 3:
Tom threw Tim three thumbtacks
Converted String 3:
tOM THREW tIM THREE THUMBTACKS

Povezano: Kako provjeriti nizove pomoću Booleovih metoda u Pythonu

Python program za pretvaranje znakova niza u suprotne slučajeve

Ispod je program Python za pretvaranje znakova niza u suprotne slučajeve:

# Python program to convert characters of string to opposite case
def convertString(str):
length = len(str)
result = ''
for i in range(length):
# If the character is in lowercase,
# convert it to uppercase
if str[i].islower():
result += str[i].upper()
# If the character is in uppercase,
# convert it to lowercase
elif str[i].isupper():
result += str[i].lower()
else:
result += str[i]
return result

str1 = 'Welcome to MUO'
print('Original String 1:')
print(str1)
print('Converted String 1:')
print(convertString(str1))
str2 = 'Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.'
print('Original String 2:')
print(str2)
print('Converted String 2:')
print(convertString(str2))
str3 = 'Tom threw Tim three thumbtacks'
print('Original String 3:')
print(str3)
print('Converted String 3:')
print(convertString(str3))

Izlaz:

Original String 1:
Welcome to MUO
Converted String 1:
wELCOME TO muo
Original String 2:
Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.
Converted String 2:
fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.
Original String 3:
Tom threw Tim three thumbtacks
Converted String 3:
tOM THREW tIM THREE THUMBTACKS

JavaScript program za pretvaranje znakova niza u suprotne slučajeve

Ispod je JavaScript program za pretvaranje znakova niza u suprotne slučajeve:

// JavaScript program to convert characters of string to opposite case
function convertString(str) {
var length = str.length;
var result = '';
for (let i = 0; i // If the character is in lowercase,
// convert it to uppercase
if (str.charAt(i) === str.charAt(i).toLowerCase()) {
result += str.charAt(i).toUpperCase();
// If the character is in uppercase,
// convert it to lowercase
} else if (str.charAt(i) === str.charAt(i).toUpperCase()) {
result += str.charAt(i).toLowerCase()
} else {
result += str.charAt(i);
}
}
return result;
}
var str1 = 'Welcome to MUO';
document.write('Original String 1:' + '
');
document.write(str1 + '
');
str1 = convertString(str1);
document.write('Converted String 1:' + '
');
document.write(str1 + '
');
var str2 = 'Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.';
document.write('Original String 2:' + '
');
document.write(str2 + '
');
str2 = convertString(str2);
document.write('Converted String 2:' + '
');
document.write(str2 + '
');
var str3 = 'Tom threw Tim three thumbtacks';
document.write('Original String 3:' + '
');
document.write(str3 + '
');
str3 = convertString(str3);
document.write('Converted String 3:' + '
');
document.write(str3 + '
');

Izlaz:

Original String 1:
Welcome to MUO
Converted String 1:
wELCOME TO muo
Original String 2:
Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.
Converted String 2:
fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.
Original String 3:
Tom threw Tim three thumbtacks
Converted String 3:
tOM THREW tIM THREE THUMBTACKS

Povezano: JavaScript nizove metoda koje biste trebali savladati danas

C Program za pretvaranje znakova niza u suprotne slučajeve

Ispod je C program za pretvaranje znakova niza u suprotne slučajeve:

// C program to convert characters of string to opposite case
#include
#include
#include
const char* convertString(char str[])
{
int length = strlen(str);
for (int i = 0; i {
// If the character is in lowercase,
// convert it to uppercase
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
// If the character is in uppercase,
// convert it to lowercase
else if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
}
return str;
}
int main()
{
char str1[] = 'Welcome to MUO';
printf('Original String 1: ⁠n');
printf('%s ⁠n', str1);
printf('Converted String 1: ⁠n');
printf('%s', convertString(str1));
char str2[] = 'Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.';
printf('Original String 2: ⁠n');
printf('%s ⁠n', str2);
printf('Converted String 2: ⁠n');
printf('%s', convertString(str2));
char str3[] = 'Tom threw Tim three thumbtacks';
printf('Original String 3: ⁠n');
printf('%s ⁠n', str3);
printf('Converted String 3: ⁠n');
printf('%s', convertString(str3));
return 0;
}

Izlaz:

Original String 1:
Welcome to MUO
Converted String 1:
wELCOME TO muo
Original String 2:
Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.
Converted String 2:
fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.
Original String 3:
Tom threw Tim three thumbtacks
Converted String 3:
tOM THREW tIM THREE THUMBTACKS

Saznajte više o manipulaciji nizovima

U ovom članku naučili ste kako pretvoriti znakove niza u suprotna slova. Bavljenje nizovima i tekstovima sastavni je dio programiranja. Morate znati rukovati nizovima.

koji iphone ima najbolju kameru

Python je solidan izbor za početak ako tražite jezik za jednostavno i učinkovito upravljanje nizovima.

Udio Udio Cvrkut E -pošta Učenje Pythona? Evo kako manipulirati nizovima

Korištenje i rukovanje nizovima u Pythonu može izgledati teško, ali je varljivo jednostavno.

Pročitajte Dalje
Povezane teme
  • Programiranje
  • Piton
  • JavaScript
  • C Programiranje
  • Vodiči za kodiranje
O autoru Yuvraj Chandra(Objavljeno 60 članaka)

Yuvraj je student preddiplomskog studija Računarstva na Sveučilištu u Delhiju u Indiji. Oduševljen je Full Stack web razvojem. Kad ne piše, istražuje dubinu različitih tehnologija.

Više od Yuvraja Chandre

Pretplatite se na naše obavijesti

Pridružite se našem biltenu za tehničke savjete, recenzije, besplatne e -knjige i ekskluzivne ponude!

Kliknite ovdje za pretplatu