Replace characters in old array - c++

I'm solving a problem and stuck on last part now what i am doing. Taking 5 characters from user and save it on character array and than saying enter 3 characters to check does array has your enter characters in it.
For example: User enter 5 characters dagpl.Than second array subArray which search characters from main array now user enter 3 charactersdgl.Result saying 3 characters found. Are you want to replace these 3 characters with new characters? So enter 3 new replace characters now user enter xyz.
Final array would be replace like this xaypz.
My Code doesn't working fine for replacing characters i don't know what i'm doing wrong.
#include<iostream>
#include<cstdlib>
using namespace std;
int main(int argc, char**argv) {
bool check = false;
char arr[6] = { '\0' };
char subarr[4] = { '\0' };
int count = 0;
cout << "Enter Characters : ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "Enter 3 Characters and see how many times does array has your Search Characters : ";
for (int i = 0; i < 3; i++) {
cin >> subarr[i];
}
//Sub Array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
if (subarr[i] == arr[j]) {
if (!check) {
cout << "Found characters are: ";
}
count++;
cout << subarr[i] << ",";
check = true;
}
}
}
if (check) {
cout << '\b';
cout << " ";
cout << endl;
}
if (!check) {
cout << "Sorry Nothing Found" << endl;
}
cout << "total Found : " << count << endl;
//SECTION 3
if (check) {
int n = count + 1;
char* replace = new char[n]();
cout << "You can only replace " << count << " new characters because of find operation! so enter it will be replace old array with it: ";
for (int i = 0; i < n - 1; i++) {
cin >> replace[i];
}
//Replace characters
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < 5; j++) {
if (subarr[i] == arr[j]) {
arr[j] = replace[j];
}
}
}
delete[]replace;
replace = NULL;
cout << "New Array would be: ";
for (int i = 0; i < 5; i++) {
cout << arr[i];
}
cout << endl;
}
system("pause");
return EXIT_SUCCESS;
}

You're not marking the matched characters from arr
Replace the matched characters from arr[j] with a distinct character so that you can determine where to replace later. you can use null terminator
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
if (subarr[i] == arr[j]) {
if (!check) {
cout << "Found characters are: ";
}
count++;
cout << subarr[i] << ",";
check = true;
arr[j]='\0'; //replacing the matched character with null terminator
}
}
}
Now traverse through your arr and replace null terminators with characters from replace array
int i=0;
for (int j = 0; j < 5; j++) {
if (arr[j]=='\0') { //replace when it equals to null terminator
arr[j] = replace[i++];
}
}

You want to ask the user to replace a specific number of chars, counting how many chars appear in subarr.
arr is input, subarr is from:
int replacecount[3] = { 0 };
for (int x=0; x<6; x++)
for (int i=0; i<3; i++)
if (input[x] == from[i]) {
++replacecount[i];
break;
}
int count = 0;
for (int i=0; i<3; i++)
if (replacecount[i] > 0)
++count;
std::cout << " you can replace " << count << " characters..." << std::endl;
char to[3];
for (int i=0; i<3; i++)
if (replacecount[i] > 0) {
std::cout << "enter replace character for '" << from[i] << "'" << std::endl;
std::cin >> to[i];
}
for (int x=0; x<6; x++)
for (int i=0; i<3; i++)
if (input[x] == from[i]) {
input[x] = to[i];
break;
}
std::cout << "replaced string is: " << input << std::endl;

Related

How can I remove the lines being output in my 2d array?

When I run the program, I get lines in between the elements of my 2d array.
How do I get rid of them? I meant to have empty values
cout << " ";
for (int i = 0; i < 10; i++)
cout << i << " ";
cout << endl;
for (int i = 0; i < 10; i++)
{
cout << i << " ";
for (int j = 0; j < 10; j++)
{
cout << grid[i][j] << " ";
}
cout << endl;
}
Doing char grid[MAX_ROWS][MAX_COLS]; is not an initialization, it just creates the grid with garbage inside it. If you want to have empty spaces you have to do something like:
for (size_t i = 0; i < MAX_ROWS; i++) {
for(size_t j = 0; j < MAX_COLS; j++) {
grid[i][j] = ' ';
}
}

Can't get output right, need same code on different line

Hey so my code works but I need to get this output right, I need it to output the letters I put in 2 times. It will print it once correctly but I added a loop and it bunches the Chars together instead of printing it 2 times in the loop. If I separate it with endl or \n it will separate the chars. I just want it to print the whole line I enter 2 times
{
char c;
string s;
int index = 0;
cout << "Enter a line:";
cin.get(c);
while (c != '\n' && index < size) {
x[index] = c;
cin.get(c);
index++;
}
Letter = index;
cout << "" << Letter << endl;
int k = 0;
for (int i = 0; i < Letter; ++i)
{
bool found = false;
for (int j = 0; j < k; ++j)
if (x[i] == x[j])
found = true;
if (!found)
x[k++] = x[i];
s = +x[i];
for (int z = 0; z < 1; z++) {
cout << "" << s;
}
}
Letter = k;
}
To read a line and print it twice:
std::string line;
if (std::cin.getline(line))
{
std::cout << line << '\n' << line << '\n';
}

Row transposition cypher using extra column

Hi guys I am trying to make a program that takes some user input and maps it to a 2d array and then encrypts it by mixing up the columns. For example if the user enters "my name is fred" the program creates an array that is 3x6 filling the last column with y's and the remain empty spaces with x's so it should be something like
mynamy
eisfry
edxxxx
instead I wind up with
mynam
eisfr
edxx
#include <iostream>
#include<cctype>
#include<algorithm>
using namespace std;
main(){
string input;
cout << "Enter information to be encrypted" << endl;
getline(cin,input);
input.erase(std::remove (input.begin(), input.end(), ' '), input.end());
int columns = 6;
int rows;
if (input.size() <= 5){
rows = 1;
}
else if (input.size()% 5 > 0){
rows = input.size()/5 + 1;
}
else
rows = input.size()/5;
char message[rows][columns];
int place = 0;
for(int i = 0; i < rows; i++){
for(int j = 0; j < (columns-1); j++){
if(place <= input.size()){
message[rows][columns] = input[place];
}
else {
message[rows][columns] = 'x';
}
place++;
message[rows][5] = 'y';
cout << message[rows][columns];
}
cout << endl;
}
}
this should do it..
#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;
int main()
{
string input;
cout << "Enter information to be encrypted" << endl;
getline(cin,input);
input.erase(std::remove (input.begin(), input.end(), ' '), input.end());
int columns = 6;
int rows;
if (input.size() <= 5){
rows = 1;
}
else if (input.size()% 5 > 0){
rows = input.size()/5 + 1;
}
else
rows = input.size()/5;
char message[rows][columns];
for(int i = 0; i < rows; i++){
for(int j = 0; j < (columns-1); j++){
if ((i*5 + j) < int(input.size())){
message[i][j] = input[i*5 + j];
}
else {
message[i][j] = 'x';
}
// place++;
if (i != rows-1) message[i][5] = 'y';
else message[i][5] = 'x';
// cout << "i: " << i << " | j: " << j << " | " << message[i][j] << endl;
}
cout << endl;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << message[i][j];
}
cout << " ";
}
cout << endl;
}
Your code isn't actually doing anything to transpose the matrix! It's writing the message into the matrix, but it's printing each entry out right after it's written, so it doesn't end up changing the order at all.
You'll need a separate set of loops to read data out of the matrix.

How can I let users to input values into the sudoku board?

I'm having trouble generating and inserting numbers inside the Board. Here is what I have so far:
enter code here
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
bool Valid_Set(int line[])
{
bool found = true;
for (int t = 1; (t <= 9 && found); ++t)
{
found = false;
for (int i = 0; i < 9; ++i)
if (line[i] == t) found = true;
}
return found; // returns true if all value 1-9 are in array.
}
bool Valid_Matrix(int sud[9][9])
{
int i, j, check[9];
bool valid = true;
// check each row
for (j = 0; (j < 9) && valid; ++j)
{
for (i = 0; i < 9; ++i)
check[i] = sud[i][j];
valid = Valid_Set(check);
}
// check each column
for (j = 0; (j < 9) && valid; ++j)
{
for (i = 0; i < 9; ++i)
check[i] = sud[j][i];
valid = Valid_Set(check);
}
// check 3x3 area
for (i = 0; (i < 9) && valid; i += 3)
{
for (j = 0; (j < 9) && valid; j += 3)
{
int t = 0;
for (int x = 0; x < 3; ++x)
for (int y = 0; y < 3; ++y)
check[t++] = sud[x + i][y + j];
valid = Valid_Set(check);
}
}
return valid;
}
void numGenerator(int A[]){
for (int i=0; i<9; i++){
A[i]=(1+rand()%9);
}
}
bool check_for_validity(int A[]){
int counter=0;
while (counter!=9){
numGenerator(A);
counter++;
}
for (int i=0; i<9; i++)
for (int j=0; j<9; j++){
if(A[j]==A[i])
numGenerator(A);
}
return true;
}
void main (){
//Descriptions and genral ideas
cout << "WELCOME TO SUDOKU " << endl;
cout << endl;
cout << "RULES: "<< endl;
cout << endl;
cout << "->You'll be given a 9x9 board with some numbers depending on which level of difficulty you choose to play."<< endl;
cout << endl;
cout << "->You have to arrange numbers from 1 to 9 so that a number shows up once in one row, one column and in a 3x3 box." << endl;
cout << endl;
cout << "So, let's get started" << endl;
cout << endl;
cout <<endl;
char dash[9][9];
for (int array=0; array<9; array++) {
for (int array2=0; array2<9; array2++) {
dash[array][array2]='_';
}
}
char row[9];
char column[9];
int num[81];
int num2[9][9];
int length;
length=strlen(row);
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified.
for (int i=0; i<length; i++) {
dash[row[i]][column[i]]=num[i]+'0';
}
//Builds the Sudoko board and outputs the full 9x9 array.
cout << " 0 1 2 3 4 5 6 7 8 " << endl;
cout << "-----------------------------------------" << endl;
int i=0;
for (int count=0; count<3; count++) {
for (int count2=0; count2<3; count2++) {
cout << "||_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";
}
cout << "||" << i << endl;
i++;
}
cout << "-----------------------------------------" << endl;
int j=3;
for (int count=3; count<6; count++) {
for (int count2=0; count2<3; count2++) {
cout << "||_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";
}
cout << "||" << j << endl;
j++;
}
cout <<"-----------------------------------------" << endl;
int z=6;
for (int count=6; count<9; count++) {
for (int count2=0; count2<3; count2++) {
cout << "||_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";
}
cout << "||" << z << endl;
z++;
}
cout << "-----------------------------------------" << endl;
for (int row = 0; row < 9; row++) {
cout << "Enter values for row " << row + 1 << " : ";
for (int col = 0; col < 9; col++)
cin >> dash[row][col];
cout << endl << endl;
}
system("pause");
}
This whole section is wrong
char row[9];
char column[9];
int num[81];
int num2[9][9];
int length;
length=strlen(row);
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified.
for (int i=0; i<length; i++) {
dash[row[i]][column[i]]=num[i]+'0';
}
You are using row and column even though they haven't got any values. Most likely this will crash your program.
Hard to know what you expected this to do. Perhaps you could explain?
Here's a suggestion for inputing values. Maybe you'll find it useful
// get the user's values
int row, column, value;
cout << "Enter a row number, column number, and value. All numbers should be between 1 and 9\n";
cin >> row >> column >> value;
// put the value in the board, add '0' to convert the integer to a digit
dash[row][column] = value + '0';

how to process an array of even numbers from a users input and display them with spaces in C++

I need help with getting this users input of an integer and retrieving the even numbers and displaying them with spaces.I already have the input processed into an array and have it reversed (thanks to stackoverflow) now need to extract the even numbers from the array and display them.
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int evenNumbers(char even[], int num[], int indexing[]);
int main()
{
char integers[5];
int numbers[5];
int even[5] = {0,2,4,6,8};
int evens;
cout << "Please enter an integer and press <ENTER>: " << endl;
for (int j = 0; j < 5; j++)
cin >> integers[j];
for (int j = 0; j < 5; j++)
{
numbers[j]= integers[j] - '0';
}
cout << endl;
for (int j = 5; j > 0; j--)
{
cout << integers[j - 1] << " ";
}
cout << endl;
//having problems finding the even numbers and displaying the even numbers
//from the users input of integers, i have only learned how to display the
//subscript by a linear search
evens = evenNumbers(integers, numbers, even);
if (evens == -1)
cout << "There are no even numbers" << endl;
else
{
cout << "The even numbers are: " << (evens + 1) << endl;
}
system("pause");
return 0;
}
int evenNumbers(char even[], int num[], int indexing[])
{
int index = 0;
int position = -1;
bool found = false;
for (int j = 0; j < 5; j++)
{
num[j]= even[j] - '0';
}
while (index < 5)
{
if (num[index] == indexing[index])
{
found = true;
position = index;
}
index++;
}
return position;
}
If you want to display the even numbers from the array integers you can use a simple for loop and if statement:
for(int i = 4; i >= 0; i--)
{
if(integers[i] % 2 == 0)
cout << integers[i] << " ";
}
Your approach is all wrong, you can't detect even numbers by searching a list, you need a mathematical test for evenness. Write a function called is_even which tests one number and returns true if it is even and false if it is not. Then you can use that function, very simply, like this
for (int j = 0; j < 5; j++)
{
if (is_even(integers[j]))
cout << integers[j] << " ";
}
cout << endl;
Now you just need to write the is_even function.
void evennumbers(int num[])
{
for(int i=0;i<5;i++)
{
if(num[i]%2==0)
cout<<num[i]<<" ";
}
}
And avoid taking input to char what if user enters a number with more than one digit
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void validNum(char valid[]);
void reverseNum(char rev[], int num2[]);
void evenNumbers(char even[], int num3[]);
void oddNumbers(char odd[], int num4[]);
int main()
{
char integer[5];
int number[5];
cout << "Your number is: ";
validNum(integer);
cout << "Your number in reverse is: ";
reverseNum(integer, number);
cout << "Even numbers: ";
evenNumbers(integer, number);
cout << endl;
cout << "Odd numbers: ";
oddNumbers(integer, number);
cout << endl;
system("pause");
return 0;
}
void validNum(char valid[])
{
char ch;
cout << "Please enter an integer and press <ENTER>: " << endl;
ch = cin.get;
while (ch < 0 || ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
{
cout << "ERROR: Please enter a positive integer and press <ENTER>: ";
for (int i = 0; i < 5; i++)
cin >> valid[i];
}
for (int j = 0; j < 5; j++)
{
cout << valid[j] - '0';
}
}
void reverseNum(char rev[], int num2[])
{
for (int j = 0; j < 5; j++)
{
num2[j]= rev[j] - '0';
}
cout << endl;
for (int j = 5; j > 0; j--)
{
cout << rev[j - 1]<< " ";
}
cout << endl;
}
void evenNumbers(char even[], int num3[])
{
for (int i = 0; i < 5; i++)
{
if (even[i] % 2 == 0)
{
cout << num3[i] << " ";
}
}
}
void oddNumbers(char odd[], int num4[])
{
for (int i = 0; i < 5; i++)
{
if (odd[i] % 2 == 1)
{
cout << num4[i] << " ";
}
}
}