in my program I've been trying to get an input from user, and check if the letters in it were small or capital, and then make a prefix array, where I would check if the number of next small and big letters (counting from the begging of the word) was the same. I wanted to do that by assignig big letters value 1 and -1 to small ones.
My problem is that after I load the word from user to my array, and then want to make prefix array, the numbers don't match - in my opinion.
My input is:
STaSzIc
And my output is:
1 1 -1 1 -1 1 -1
1 1
2 2
0 3
0 4
0 5
0 6
0 7
Why after 3'rd value, next don't work? Here's my code:
#include <iostream>
using namespace std;
int main()
{
string Caps;
int tab[1000000];
cin >> Caps;
for(int i = 1; i <= Caps.length(); i++){
if(Caps[i - 1] <= 90){
tab[i] = 1;
cout<<tab[i]<<' ';
}else{
tab[i] = -1;
cout<<tab[i]<<' ';
}
}
cout<<endl;
cout<<endl;
int prefiksy[1000000];
for(int i = 1; i <= Caps.length(); i++){
prefiksy[i] = tab[i] + tab[i - 1];
cout<<prefiksy[i]<<" "<<i<<endl;
}
}
I was hoping for such result:
1 1 -1 1 -1 1 -1
1 1
2 2
1 3
2 4
1 5
2 6
1 7
+ tab[i - 1];
you don't initializetab[0]
before use.tab[i] + tab[i - 1]
only calculates the difference between the current and previous element, you don't keep a total tally if that's what you meanprefiksy
array is useless since you never read from it, maybe that's the error