c++ program need help strings

Pages: 12
hi i am a beginner and we just started learning about strings. i am supposed to write a program that creates two strings of size 100 and then the first part of the program prints out only capital letters(if the character isnt a letter dont print if the character is lower case then change it to uppercase and print)
the second part of the program checks all the charecters between two seperate strings. whatever character that is in the second string and also in the first we erase. i would appreciate some help. thank you


input : abcabc
a

output : ABCABC

@
@
@



expected output: ABCABC
bcbc


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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86




//---------------------------------include's---------------------------------//

#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <iomanip>
//-----------------------------------using's---------------------------------//

using std::cin;
using std::cout;
using std::endl;
//using std::string;
//using std::getline;

//------------------------------------const----------------------------------//

char const MAX_STR_LEN = 100;

//-----------------------------------main------------------------------------//


int main()
{
	char str1[MAX_STR_LEN], str2[MAX_STR_LEN];
	int i=0;


	cin.getline (str1,MAX_STR_LEN);

	cin.getline (str2,MAX_STR_LEN);


	do
	{
		if ( isalpha(str1[i]) )
		{
			if ( str1[i] >= 'a' && str1[i] <='z' )
			{
				cout << (char) ( str1[i] -32) ;
			}
			else
			    cout << (char) str1[i];
		}
		i++;



    }
	while(str1[i] != '\0');

	cout << endl;

	for (int i = 0; i < MAX_STR_LEN && str1[i]!='\0';i++)
	{

		for (int j = 0; j < MAX_STR_LEN && str2[j]!='\0'; j++)
		{
			if(str1[i] == str2[j])

			{
				str1[i] = 0;


			}

		}

	}

	for (int i=0;i<MAX_STR_LEN;i++)
	{
		cout<<(char) (str1[i]);
	}


	return EXIT_SUCCESS;

}


Last edited on
Hi @user42,
First I'm going to present the ASCII codes:
'a'==97
'z'==122
'A'==65
'Z'==90
Therefore uppercase is [65, 90], and lowercase [97, 122].
If you want to turn a lowercase letter to an uppercase letter, then subtract 32 from the component.
Try 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
27
28
29
//this is manual, but you can do it either way. I just trust doing it manually more.
int length1, length2;
length1=strlen(str1);
length2=strlen(str2);
for (int i=0; i<length1; i++)
{
    if (str1[i]>='a' && str1[i]<='z')
    {
        cout<<str1[i]-32;
    }
    else
    {
        cout<<str1[i];
    }
}
for (int i=0; i<length1-1;)
{
    for (int j=0; j<length2-1; j++)
    {
        if (str1[i]==str2[j])
        {
            strcpy(str1+i, str1+i+1); //eliminates the character on i.
        }
        else
        {
            i++;
        }
    }
}

If you use "strlen()", then you get the number of components from your input. if you just check all 100 components, you will get past what you've inputted.
For the word "asd", you have:
str[0]=='a'
str[1]=='s'
str[2]=='d'
str[3]=='\0'
str[4] until str[99]==random values
Hope this helps.
Sorry about that... I wasn't aware of some things that happen in the compiler.
This one should work:
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
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
char const MAX_STR_LEN = 100;
int main()
{
	char str1[MAX_STR_LEN], str2[MAX_STR_LEN], backup[MAX_STR_LEN];
	cin.getline (str1,MAX_STR_LEN);
	cin.getline (str2,MAX_STR_LEN);
	int length1, length2, i;
    length1=strlen(str1);
    length2=strlen(str2);
    for (i=0; i<length1; i++)
    {
        if (str1[i]>='a' && str1[i]<='z')
        {
            cout<<char(str1[i]-32);
        }
        else if (str1[i]>='A' && str1[i]<='Z')
        {
            cout<<str1[i];
        }
    }
    i=0;
    while (i<length1)
    {
        for (int j=0; j<length2; j++)
        {
            if (str1[i]==str2[j] && str1[i]>='a' && str1[i]<='z')
            {
                strcpy(backup, str1+i+1);
                strcpy(str1+i, backup);
            }
            else
            {
                i++;
            }
        }
    }
    cout<<'\n'<<str1;
}
update................
the first part of the program is now working
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
char str1[MAX_STR_LEN], str2[MAX_STR_LEN];
	int i=0;


	cin.getline (str1,MAX_STR_LEN);

	cin.getline (str2,MAX_STR_LEN);


	do
	{
		if ( isalpha(str1[i]) )
		{
			if ( str1[i] >= 'a' && str1[i] <='z' )
			{
				cout << (char) ( str1[i] -32) ;
			}
			else
			    cout << (char) str1[i];
		}
		i++;



    }
	while(str1[i] != '\0');

	cout << endl;


    ABCABC 



the second part is still not working.
i basically want to remove the charecter from str1 that is similar to str2 (on line 62)
i know that there is some function to remove a character but i do not know how to use it
If you use std::string, then it's just one function. If you use char arrays (like in this case), it's either:
 
strcpy(str1+i, str1+i+1);

Or
1
2
3
//this is sometimes safer
strcpy(backup, str1+i+1);
strcpy(str1+i, backup);


EDIT:
And the
 
while(str1[i] != '\0');

is an infinite loop. It basically checks 1 value over and over again, but does nothing since there's no execution before ';'.
Last edited on
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
//---------------------------------include's---------------------------------//

#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <iomanip>
#include <string.h>
//-----------------------------------using's---------------------------------//

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;


//------------------------------------const----------------------------------//

char const MAX_STR_LEN = 100;

//-----------------------------------main------------------------------------//


int main()
{
	char str1[MAX_STR_LEN], str2[MAX_STR_LEN];
	int i=0,count=0;


	cin.getline (str1,MAX_STR_LEN);

	cin.getline (str2,MAX_STR_LEN);


	do
	{
		if ( isalpha(str1[i]) )
		{
			if ( str1[i] >= 'a' && str1[i] <='z' )
			{
				cout << (char) ( str1[i] -32) ;
			}
			else
			    cout << (char) str1[i];
		}
		i++;



    }
	while(str1[i] != '\0');

	cout << endl;

//----------------------------------part2------------------------------------//


	for (int i = 0; str1[i]!='\0'; i++)
	{

		for (int j = 0; str2[j]!='\0'; j++)
		{
			if(str1[i] != str2[j])
			{
				count++;
			}


		}
		if(count==strlen(str2))
		{
			cout<< str1[i];
		}
		count=0;

	}


	return EXIT_SUCCESS;

}






this is the newly edited code and i believe that it now works. my question is if i have any usings or includes that are not needed. plus on line 72 there is a warning that says "comparison between signed and unsigned integer expression" what exactly does this mean?
Last edited on
 
#include <string> 

This is for std::string. You might want to use
1
2
3
#include <cstring>
//or
#include <string.h> 

For char arrays.

As for the comparison, it means this:
count is an int variable, and strlen() is a function that returns an unsigned-int value. It means that sometimes it's better to use the same data type for both.
In your case, it means that you should declare
 
unsigned count;

instead of
 
int count;
got it and are the following things redundant?

using std::string;
using std::getline;
#include <cctype>
#include <iomanip>
#include <cstdlib>

std::string is the one you don't use here
std::getline is an overload for std::string, so you don't need it here
<cctype> contains isalpha(), isupper() and islower(), so you need it
<iomanip> manipulates precision in float numbers and outputs, you don't need it here
<cstdlib> contains various things, also not needed it here
i believe i need the <cstdlib> for the return EXIT_SUCCESS
That I don't know. I don't use a return in my main() unless i'm in a loop that I want to get out of and close the program. When I did use return, I would just return 0 or 1. But if you want to use EXIT_SUCCESS, then it's for the best. Who knows when you remember this and helps you solve something impossible.
Last edited on
ok.
i have another program im working on that doesnt have to do with strings, do you mind having a look at it?
http://www.cplusplus.com/forum/beginner/206189/
I will look upon it, but not necessarily now. I, myself, am trying to solve a problem. If I can't look now, I will in about 10-12 hours.
If I write my own code to solve it, is it all right?
Or do you want just yours to be solved so you can continue with the style?
I would rather with mine because I think I would understand it more. But if it is to much trouble then I wouldn't mind yours. I just haven't learned many new and different things so I probably won't understand every function or command you use
Actually don't mind what I've said then, because now I understand better what you wrote. I was kind of tired, so I didn't really see why you (unimportant).
Anyway, here's the code. It should work, but from what I've seen on your post, you have inputted a 9x6 Sudoku grid. Shouldn't it be 9x9? Because when I opened the program, it wanted me to write 81 numbers, not just 54.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <cstdlib>
using std::cin;
using std::cout;
const int N = 3;
void get_matrix(int matrix[][N*N]);
bool row_ok(int matrix[][N*N],int row);
bool col_ok(int matrix[][N*N],int col);
bool square_ok(int matrix[][N*N], int box_row,int box_col);
int main()
{
	int matrix[N*N][N*N],count=0;
	bool check_col=true,check_row=true,check2=true,check4=true,check_sub_square=true;
	get_matrix(matrix);
	/* this is so that you can visualize the matrix
	cout<<"\n\n\n";
	for (int i=0; i<N*N; i++)
	{
	    for (int j=0; j<N*N; j++)
	    {
	        cout << matrix[i][j] << ' ';
	    }
	    cout<<'\n';
	}
	*/
	check2=square_ok(matrix,7,4);
	if (check2)
	{
	    cout<<"true\n";
	}
	else
	{
	    cout<<"false\n";
	}
	for (int i=0;i<N*N && check_row;i++)
    {
        check_row = row_ok(matrix,i);
    }
    for (int j=0;j<N*N && check_col && check_row ;j++)
    {
        check_col = col_ok(matrix,j);
    }
	for (int i = 0 ;i<N && check4; i++)
	{
		for(int j=0;j<N && check4; j++)
		{
			check4 = square_ok(matrix,i*N+1,j*N+1);
			if (check4)
			{
				count++;
			}
			else
			{
				check4=false;
				break;
			}

		}
	}
	if (count==N*N)
	{
		check_sub_square=true;
	}
	else
	{
		check_sub_square=false;
    }
	if (!check_row || !check_col || !check_sub_square)
    {
        cout<<"0\n";
    }
    else
    {
        cout<<"1\n";
    }
}
void get_matrix(int matrix[N*N][N*N])
{
    int i,j,temp=0;
	for (i=0; i<N*N; i++)
	{
		for (j=0; j<N*N; j++)
		{
        //I added the cout so you can keep track of numbers at input
            cout << "Row " << i+1 << " column " << j+1 << ": ";
			cin >> temp;
			if(temp>=0 && temp<=N*N)
			{
				matrix[i][j]=temp;
			}
			else
			{
			    j--;
			}
		}
	}
}
bool row_ok(int matrix[][N*N],int row)
{
    //here you have to change: row must be in the first set of brackets.
    //that is because point[i][j] means it has i lines and j columns.
	for(int i=0; i<N*N; i++)
	{
		for(int j=(N*N)-1; j>=i; j--)
    //j>=i will reduce the number of executions 36 times when the grid is valid.
    //just like you did in square_ok
		{
			if (matrix[row][i]!=0)
			{
				if (matrix[row][i]==matrix[row][j] && i!=j)
				{
					return false;
				}
			}
		}
	}
	return true;  //else the function returns true
}
bool col_ok(int matrix[][N*N],int col)
{
    //here you have to change: col must be in the second set of brackets.
    //that is because point[i][j] means it has i lines and j columns.
	for(int i=0; i<N*N; i++)
	{
		for(int j=(N*N)-1; j>=i; j--)
    //j>=i will reduce the number of executions 36 times when the grid is valid.
    //just like you did in square_ok
		{
			if (matrix[i][col]!=0)
			{
				if (matrix[i][col]==matrix[j][col] && i!=j)
				{
					return false;
				}
			}
		}
	}
	return true;
}
//this has some comparing bugs, try this
bool square_ok(int matrix[][N*N],int box_row,int box_col)
{
	int remainder1,remainder2,corner_row,corner_col;
	remainder1 = box_row %N;
	remainder2 = box_col %N;
	int corner_row_holder=box_row - remainder1
			,corner_col_holder =box_col - remainder2 ;
	for(corner_row=corner_row_holder ;corner_row<corner_row_holder+N; corner_row++)
        {
		for(corner_col=corner_col_holder ;corner_col<corner_col_holder+N; corner_col++)
		{
			for (int i=corner_row_holder+N-1; i>=corner_row_holder; i--)
			{
				for (int j=corner_col_holder+N-1; j>=corner_col_holder; j--)
				{
					if (matrix[i][j]!=0 && i!=corner_row || j!=corner_col)
					{
						if (matrix[i][j]==matrix[corner_row][corner_col])
						{
							return false;

						}
					}

				}
			}
		}
	}
	return true;
}

P.S.: Now that I understand how you tried to solve the problem. Not only that, but I even liked very much the concept of taking the right-bottom corner and check the cells backwards.
Last edited on
it works!
thanx so much for all your help troat, im gonna go over this and try and understand the changes you've made
btw your'e really good at this
good luck with you're project!
Last edited on
i need to enetr the whole algorithm into a function, anyone have any ideas?
You could try this: copy+paste the code from each function into one common function. Instead of returning a value, you use a variable:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int valid=0;
(code)
if (condition to check whether the column/line/square is NOT a valid one)
{
    cout<<"This cannot be qualified for a sudoku game.\n";
    cin.get(); //waits for the user to press enter
    cin.get(); //the second one is to make sure the program waits.
    //sometimes it doesn't work if you use one and also inputted something before.
    return 0;
}
else //if the column/line/square is good to go
{
    valid=true;
}

That way if the values are not valid, you don't have to check any further. You just know that at least 1 condition is not met.
Do this with all the functions, and you're good to go.
(You can put it all in main(), so the program closes if it's not valid. You can check for bugs faster.)
why do I need twice cin.get
Pages: 12