#include <iostream>
#include <cstring>
using namespace std;
int main() {
char main[10000] = {0};
cin.getline(main, 10000);
char clone [10000] = {0};
int n;
cin >> n;
int counter = 0;
int counter2 = 0;
for (int z = 0 ; z < strlen(main); z++) {
if (main[z] == '.' || main[z] == ',' || main[z] == ' ' || main[z] =='"' ||
main[z] == '!' || main[z] == '?' || main [z] == ';')
counter2++;
}
if (counter2 == 0) {
if (strlen(main) < n) {
n = n % strlen(main);
}
int b = strlen(main);
for (int x = b - n; x < b ; x++) {
cout << main[x];
}
for (int y = 0; y < x - n; y++) {
cout << main[y];
}
}
for (int i = 0; i < strlen(main); i++) {
clone[counter] = main[i];
if(main[i] == '.' || main[i] == ',' || main[i] == ' ' || main[i] =='"' ||
main[i] == '!' || main[i] == '?' || main [i] == ';') {
int a = strlen(clone);
if (counter < n) {
n = n % counter;
}
for (int x = a - n - 1; x < a - 1; x++) {
cout << clone[x];
}
for (int y = 0; y < x - n; y++) {
cout << clone[y];
}
cout << main[i];
counter = 0;
for (int z = 0; z < a; z++) {
clone[z] = '\0';
}
}
else {
counter++;
}
}
return 0;
}
I'm working on a project that switches letters in words. For example the word funny when n = 3 will output nnyfu. It just moves all letters n-times. However when I try to do this with a long sentence containing a character different than a letter and follow it with another char of the same type (for example a coma followed by space) the program seems to crash. I'm wondering if anyone can help me with this because I've been pulling my hair out all day long. Also it mustn't be necessary for an input with a couple of words to end with a char different than a letter but I can't seem to figure it out too (for example inputting "Hello my friends" and n = 3 it will output "lloHe ym).
Related
I take input from the user, calculate the number of rows and columns needed and put all the numbers in an array but it puts seemingly random values, it also duplicates some values i want to take the input from the user and put all the numbers in 2d array in order to do matrix operations
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define max_input_limit 99
float matrix_1[100][100];
float matrix_2[100][100];
float result[100][100];
int rows_1 = 0;
int cols_1 = 0;
void str_to_array1(char str[max_input_limit])
{
// sample input: [1 2 3, 4 5 6, 7 8 9]
// count number of commas
int rows = 1;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == ',')
rows++;
}
rows_1 = rows;
// count number of spaces before first comma
int cols = 1;
for (int i = 0; str[i] != ','&&i<strlen(str); i++)
{
if (str[i] == ' ')
{
cols++;
}
}
cols_1 = cols;
//remove all commas and brackets from str
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == ',' || str[i] == '[' || str[i] == ']')
{
str[i] = ' ';
}
}
// fill 2D array
float arr[100][100];
for (int i = 0; str[i] != '\0'; i++)
{
for (int r = 0; r <= rows; r++)
{
for (int c = 0; c < cols; c++)
{
if (str[i] != ' ' && str[i+1] != NULL)
{
arr[r][c] = atof(&str[i]);
matrix_1[r][c] = arr[r][c];
cout << matrix_1[r][c]<<" ";
//if (arr[r][c] == rows+r)
//cout << ",";
i++;
}
}
}
}
}
void str_to_array2(char str[max_input_limit])
{
// sample input: [1 2 3, 4 5 6, 7 8 9]
// count number of commas
int rows = 1;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == ',')
rows++;
}
rows_1 = rows;
// count number of spaces before first comma
int cols = 1;
for (int i = 0; str[i] != ','; i++)
{
if (str[i] == ' ')
cols++;
}
cols_1 = cols;
//remove all commas and brackets from str
for (int i = 0; i < strlen(str); i++)
{
if (str[i] == ',' || str[i] == '[' || str[i] == ']')
{
str[i] = ' ';
}
}
// fill 2D array
float arr2[100][100];
for (int i = 0; str[i] != '\0'; i++)
{
for (int r = 0; r <= rows; r++)
{
for (int c = 0; c < cols; c++)
{
if (str[i] != ' ' && str[i + 1] != NULL)
{
arr2[r][c] = atof(&str[i]);
matrix_2[r][c] = arr2[r][c];
cout << matrix_2[r][c]<<" ";
//if (arr[r][c] == rows+r)
//cout << ",";
i++;
}
}
}
}
}
void add(float result[100][100], float matrix_1[100][100], float matrix_2[100][100])
{
cout << "[";
for (int r = 0; r < rows_1; r++)
{
for (int c=0; c < cols_1; c++)
{
result[r][c] = matrix_1[r][c] + matrix_2[r][c];
cout << result[r][c] << " ";
}
}
cout << "]";
}
int main()
{
char str[99];
char str2[99];
cin.getline(str, 99);
str_to_array1(str);
cin.ignore();
cin.getline(str2, 99);
str_to_array2(str2);
}
When i put
[1 2 33, 4 55 6]
I get output
[1 2 33 3 4 55 5 6 ]
I've been trying to figure out the problem behind this for a few days. I think it's counting the neighbours incorrectly because when I print the counts, the numbers are mostly 1s and 2s and my output board is completely blank. X ('X') means alive and ' ' means dead.
void NextGen(char lifeBoard[][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard) {
char nexGenBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];
// initialize nexGenBoard to blanks spaces
for(int i = 0; i < numRowsInBoard; i++) {
for(int j = 0; j < numColsInBoard; j++) {
nexGenBoard[i][j] = {' '};
}
}
// start from i = 1 and j = 1 to ignore the edge of the board
for(int i = 1; i < numRowsInBoard-1; i++) {
for(int j = 1; j < numColsInBoard-1; j++) {
int count = 0;
for(int y = -1; y < 2; y++) {
for(int x = -1; x < 2; x++) {
if(!(x == 0 || y == 0)) {
if(lifeBoard[i+y][j+x] == X) //X is a global constant of 'X'.
{
count++;
}
}
}
}
if(lifeBoard[i][j] == X) {
if(count == 2 || count == 3) {
nexGenBoard[i][j] = X;
}
}
else if(lifeBoard[i][j] == ' ') {
if(count == 3) {
nexGenBoard[i][j] = X;
}
}
}
}
for(int i = 0; i < numRowsInBoard; i++) {
for(int j = 0; j < numColsInBoard; j++) {
lifeBoard[i][j] = nexGenBoard[i][j];
}
}
}
Your check during counting (!(x == 0 || y == 0)) is wrong. This will not check the square if either x or y is zero. You want to not count if both x and y are zero.
if (!(x == 0 && y == 0))
or
if (x != 0 || y != 0)
I'm trying to print a hollow square. I currently have the top, bottom, and left borders drawn but for some reason cannot get the right side to draw in the correct place. I'm sure its a simple fix but I'm new to this so sorry for noobness.
int main()
{
const int boardX = 10;
const int boardY = 10;
char gameBoard[boardX][boardY];
for (int i = 0; i != boardX; i++)
{
for (int k = 0; k != boardY; k++)
{
if (i == 0 || i == 9 || k == 0 || k == 9)
{
gameBoard[i][k] = '*';
cout << gameBoard[i][k];
}
}
cout << "\n";
}
system("pause");
return 0;
}
You forgot to print spaces to make the square hollow:
int main()
{
const int boardX = 10;
const int boardY = 10;
char gameBoard[boardX][boardY];
for (int i = 0; i != boardX; i++)
{
for (int k = 0; k != boardY; k++)
{
if (i == 0 || i == 9 || k == 0 || k == 9)
{
gameBoard[i][k] = '*';
cout << gameBoard[i][k];
}
else
{
gameBoard[i][k] = ' ';
cout << gameBoard[i][k];
}
}
cout << "\n";
}
return 0;
}
Note: I followed your style to make the changed part stand out, but normally you would separate the construction of the board and printing it to the screen into separate code blocks/functions.
what I want to do is :
Input a sentence from the user. Use full stop, space and comma as word separators. Each word should be stored in a 2D array whose columns vary in size and each row stores one word as a NULL terminated string.
For example, if the user inputs:
Hello how are you?
It should be stored as:
H E l l o NULL
h o w NULL
a r e NULL
y o u ? NULL
so whenever I try to run my code either this error appears
Exception thrown at 0x00832605 in Project109.exe: 0xC0000005: Access violation writing location 0xFDFDFE03.
or the program stops working.major problem is in
ptr[i][j] = str1[j];
`
char str1[20];
cin.get(str1, 20);
int len, sum = 0;
len = strlen(str1);
int i = 0;
while (str1[i] != '\0')
{
if (str1[i] == ' ' || str1[i] == '.' || str1[i] == ',' || str1[i] == '?' || str1[i] == ';')
{
sum = sum + 1;
}
i++;
}
char **ptr;
ptr = new char*[sum];
for (int i = 0; i < sum; i++)
{
ptr[i] = new char[20];
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
ptr[i][j] = '\0';
}}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
for (int i = 0; i < sum; i++)
{
int j = 0;
while (ptr[i][j] != '\0')
{
cout << ptr[i][j];
j++;
}
}
for (int i = 0; i < sum; i++)
{
delete[] ptr[i];
}
delete[] ptr;
system("pause");
return 0;}
`
You are indexing out of range, and hitting memory with fence bytes containing 0xFD.
Consider the loop here
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < 20; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
If i is already at (or near) it's maximum value, in the inner loop you might increment it one or more times before reaching ptr[i][j] = str1[j];. At that time i might be way more than sum.
better solution but output is not that as required :
{ char str1[20];
cin.get(str1, 20);
int len, sum = 0;
len = strlen(str1);
int i = 0;
while (str1[i] != '\0')
{
if (str1[i] == ' ' || str1[i] == '.' || str1[i] == ',' || str1[i] == '?' || str1[i] == ';')
{
sum = sum + 1;
}
i++;
}
char **ptr;
ptr = new char*[sum];
for (int i = 0; i < sum; i++)
{
ptr[i] = new char[len];
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < len; j++)
{
if (str1[j] == ' ' || str1[j] == '.' || str1[j] == ',' || str1[j] == '?' || str1[j] == ';')
{
ptr[i][j] = str1[j];
i++;
}
else
{
ptr[i][j] = str1[j];
}
}
}
for (int i = 0; i < sum; i++)
{
for (int j = 0; j < len; j++)
{
cout << ptr[i][j];
}
cout << endl;
}
for (int i = 0; i < sum; i++)
{
delete[] ptr[i];
}
delete[] ptr;
system("pause");
return 0;}
Im trying to mark the path through the maze. I'm stucked at this point. It only mark one way to the right. But the position when i write it out is X=1 Y=2. Its really strange. My idea was to make a while loop for the if statements but then the if statements not even run.. Please give me some ideas.
if (strcmp(argv[1], "-input") == 0)
{
ifstream ifs(argv[2]);
if(!ifs)
{
cout << "Felaktig textfil";
}
vector<vector<char>> maze;
vector<vector<char>> path;
string line;
unsigned int j=0;
while (!ifs.eof())
{
getline(ifs, line);
maze.push_back(vector<char>());
for (int i=0; i < line.size(); i++)
{
maze[j].push_back(line[i]);
}
j++;
}
//cout << maze.size();
int startX,startY;
for (int i = 0; i < maze.size(); i++)
{
for (int j = 0; j < maze[i].size(); j++)
{
if (maze[i][j] == 'S')
{
startX = j;
startY = i;
break;
}
}
}
if (startX + 1 < maze.size() && maze[startX + 1][startY] != '*')
{
startX++;
maze[startY][startX]='x';
}
if (startX - 1 > 0 && maze[startX - 1][startY] != '*')
{
startX++;
maze[startY][startX] = 'x';
}
if (startY - 1 > 0 && maze[startX][startY - 1] != '*')
{
startY--;
maze[startY][startX] = 'x';
}
if (startY + 1 < maze.size() && maze[startX][startY + 1] != '*')
{
startY++;
maze[startY][startX]='x';
}
if (maze[startY][startX]=='X')
{
cout << "done";
}
cout << startX <<startY << endl;
PrintMaze(maze);
}