Zamiana znaków- string

0

Cześć wszystkim ! Mam mały problem z zamianą znaków w łańcuchu string- miałem przekazać referencje do obiektu string jako parametr i zamienić zawartość łańcucha na wielkie litery. Oto mój kod:

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
void version1( string & s1);
int main()
{

    string napis;;
    int i=0;
    char c;
    cout<<"Podaj lancuch ";
    getline(cin,napis);
    cout<<"Wprowadzony lancuch to ";
    cout<<napis;
    
    version1(napis);

    return 0;
}
void version1( string & s1)
{
    int i=0;
    char c;

    for(i=0;i<10;i++)
    {
        c=s1[i];
      cout<<toupper(c);

    }
}
 

Wypisuje mi zamiast tego jakieś liczby, które raczej nie odpowiadają znakom ASCII :P
Macie jakiś pomysł jak sobie z tym poradzić ??

1

Wystarczy w dokumentację zajrzeć.

The uppercase equivalent to c, if such value exists, or c (unchanged) otherwise. The value is returned as an int value that can be implicitly casted to char.

Czyli

cout<<(char)toupper(c);

powinno załatwić sprawę.

0
Według cplusplus.com:
The uppercase equivalent to c, if such value exists, or c (unchanged) otherwise. The value is returned as an int value that can be implicitly casted to char.

Musisz zrobić konwersje na char: cout << char(toupper(s[i])); lub jak u Ciebie : cout << char(toupper(c));

1
 
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
void version1( string & s1);
int main()
{
 
    string napis;;
    int i=0;
    char c;
    cout<<"Podaj lancuch ";
    getline(cin,napis);
    cout<<"Wprowadzony lancuch to ";
    
 
    version1(napis);
 system("pause");
    return 0;
}
void version1( string & s1)
{
    int i=0;
    char c;
 
    for(i=0;i < s1.length();i++)
    {
        c=s1[i];
      cout<<(char)toupper(s1[i]);
 
    }
}

0

możesz też tak:

#include <iostream>
#include <string>
#include <algorithm>

void strToUpper(std::string& str) {
	std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}

int main() {
	std::string str;
	std::cout << "Podaj lancuch: ";
	getline(std::cin,str);
	strToUpper(str);
	std::cout << str << std::endl;
}
0

Ok, ok. Dzięki, będę pamiętać na przyszłość :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1