String, char

Hello,im begginer and i have problem with one of my homework, i have no idea how string and char works, i searching it for around 2 hours but still dont get succesfull output. I will be very glad for any advice on how to do it.

"Develop a program that performs character substitutions. Uppercase characters shall be converted to lowercase,
lowercase characters shall be converted to uppercase and digits shall be replaced by lowercase letters.
The input of your program is a single word, the word is at most 80 characters long."

Sample input & output of program:

abcABC012
ABCabcabc

Headasulus
hEADASULUS
Are you allowed to use std::string or does your professor want you to do it the old deprecated way with character arrays?
well we "using namespace std;" and then write commands without "std::" so i think we can use it :)
Last edited on
show us what you have got so far and then we can help further.
well that's my problem, i don't know much about technical terms in this links, i check and read it all but its a little hard for me understand how this commands works... all at all i know i must got some string from input and then somehow swap ascii but i have no idea how i should do it in string.
start should looks like something similiar to this, but i dont know how to make from symbols in string another symbols attributable ascii value. For example i have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const int longed=80;
int main()
{
    cout << "Enter string: " << endl;
    char rez[longed];
    cin >> rez;

    cout << rez << '\n' + '32' << endl;
    return 0;
}


cause symbol "a" have ascii 97 and "A" 65 so i need move ascii value +32 in every symbol in string and this is what i dont know how to do.. hope you understand :(
Those links I gave you handle all that for you and more.

For a start, try removing line 6 and replacing line 10 with a string object instead of a character array.
but still i dont know how add ascii value when i got it from every symbol in string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

void prevadec(string letter)
{
    for (int i = 0; i < letter.length(); i++)
    {
        char x = letter.at(i);
        cout << int(x) << endl;
    }
}

int main()
{
   string plainText;
    cout << "enter string: ";
    getline(cin, plainText);
    prevadec(plainText);
    return 0;
}

something like
1
2
3
    if (islower(x))
        cout << toupper(x);
//  else if (... etc) 

To Chervil:
Well if i use if else if with islower etc. i cant get from this 0123456789 that abcdefghij.. i can get this only if i change ascii
Focus on changing the case of letters first, then you can deal with the numbers to letters thing after you've got the rest working.
I doing this for like 5 hours and im just getting be more lost... have no idea how to make it even i read this links for so many times. I can do from something like ABCD this:
a
b
c
d

but it should be in string and not in every letter in single line, i dont know how to put it there..well i probebly dont have homework but still.. thanks atleast for some advice :/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

void prevadec(string letter)
{
    for (int i = 0; i < letter.length(); i++)
    {
        char x = letter.at(i);
        int cislo = int(x) + 32 ;
        cout << static_cast<char>(cislo) << endl;

    }
}

int main()
{
   string plainText;
    cout << "write what you want convert: ";
    getline(cin, plainText);
    prevadec(plainText);
    return 0;
}
Last edited on
Start with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <locale>
#include <cctype>

std::string convertString(std::string s)
{
    for(std::string::iterator it = s.begin(); it != s.end(); ++it)
    {
        if     (isupper(*it)) *it = tolower(*it);
        else if(islower(*it)) *it = toupper(*it);
        else if(isdigit(*it))
        {
            //write code here...
        }
    }
    return s;
}

int main()
{
    std::cout << "Input string to convert: " << std::flush;
    std::string toconvert;
    std::getline(std::cin, toconvert);
    std::cout << "Converted string: " << convertString(toconvert) << std::endl;
}
http://ideone.com/8TxWUs
String is a class that contains a sequence of char and provides methods to do all the basic work you might do with a char. So:

String aString = "My String";

if (isupper(aString[index]))
aString[index] = tolower(aString[index]));

// aString[index] - the operator [] has been overridden in String to treat the string like an array.
// tolower and isupper are defined in the cctype header to deal with the integer equivalent of a character which is required to do the "math".
// note: the operator '=' has also been overridden for dealing with a String.
Last edited on
to work only with the first word of the string change line 24 to this...

std::getline(std::cin, toconvert, ' ');
Manga wrote:
to work only with the first word of the string change line 24 to this...

std::getline(std::cin, toconvert, ' ');
Or you could use std::cin >> toconvert;...
Topic archived. No new replies allowed.