Please Help!

I want to find the lowest price i need to pay to convert text so that every
word has only upper or lower case letter.Converting 'A' into 'a' costs 1 $ and 'Z' into 'z' costs 26$.Converting 'a' into 'A' costs 26$ and 'z' into 'Z' costs 1$.There is only one space between the words in the text.
I think the program that i wrote should be working just fine but every time i get different results.
Can anyone help me with this?I have been stuck trying to find my mistakes for hour.

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
27
28
29
#include<iostream>
#include<string>
using namespace std;
int main()
{
    int sumcap=0,sumlow=0,total=0;
    string text;
    getline(cin,text);
    for(int i=0; i<text.size(); i++)
    {
        while(text[i]!=' ')
        {
            if(isalpha(text[i])&&isupper(text[i]))
            {
                sumcap+=text[i]-'A'+1;
            }
            else
            {
                sumlow+='z'-text[i]+1;
            }
            i++;
        }
        if(sumcap<sumlow)total=total+sumcap;
        else total=total+sumlow;
    }
    cout<<total;
    return 0;
}
Last edited on
> while(text[i]!=' ')
¿does your input ends with whitespace? for example "hello world "
otherwise, you'll be accessing out of bounds
Thanks for the advice but still it is giving me different results every time.Like 28 after i run it again it is 14 etc. and i dont change anything in the programm
I added some cout statements to walk through the code using "Hello World " as input. Are these the numbers you expect to see?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
Hello World // space after last character there

Starting for loop with character 0: H
	Starting while loop with character 0: H
		isalpha_upper: 8 being added to sumcap
		i in while loop is now 1
	Starting while loop with character 1: e
		not isalpha_upper: 22 being added to sumlow
		i in while loop is now 2
	Starting while loop with character 2: l
		not isalpha_upper: 15 being added to sumlow
		i in while loop is now 3
	Starting while loop with character 3: l
		not isalpha_upper: 15 being added to sumlow
		i in while loop is now 4
	Starting while loop with character 4: o
		not isalpha_upper: 12 being added to sumlow
		i in while loop is now 5
	sumcap: 8 < sumlow 64
	total is now 8
Starting for loop with character 6: W
	Starting while loop with character 6: W
		isalpha_upper: 23 being added to sumcap
		i in while loop is now 7
	Starting while loop with character 7: o
		not isalpha_upper: 12 being added to sumlow
		i in while loop is now 8
	Starting while loop with character 8: r
		not isalpha_upper: 9 being added to sumlow
		i in while loop is now 9
	Starting while loop with character 9: l
		not isalpha_upper: 15 being added to sumlow
		i in while loop is now 10
	Starting while loop with character 10: d
		not isalpha_upper: 23 being added to sumlow
		i in while loop is now 11
	sumcap: 31 < sumlow 123
	total is now 39
final total is 39

That is right that is what I am expecting.But when i try with "aA zZ" it should be 2 and i get 28 or when i try with "You Are NoT Alone" i get 25 or 24 or something around 20 and it should be 32.
It sounds like you want to reset sumcap and sumlow to 0 at the beginning of the for loop. So the cost of changing each word is evaluated separately.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
aA zZ

Starting for loop with character 0: a
	Starting while loop with character 0: a
		not isalpha_upper: 26 being added to sumlow
		i in while loop is now 1
	Starting while loop with character 1: A
		isalpha_upper: 1 being added to sumcap
		i in while loop is now 2
	sumcap: 1 < sumlow: 26
	total is now 1
Starting for loop with character 3: z
	Starting while loop with character 3: z
		not isalpha_upper: 1 being added to sumlow
		i in while loop is now 4
	Starting while loop with character 4: Z
		isalpha_upper: 26 being added to sumcap
		i in while loop is now 5
	sumcap: 26 not < sumlow: 1
	total is now 2
final total is 2
Last edited on
> But when i try with "aA zZ"
> or when i try with "You Are NoT Alone"
Again, while(text[i]!=' ') will step out of bounds
Topic archived. No new replies allowed.