# Thread: transforming string to 2D array..

1. ## transforming string to 2D array..

I got a string of maximum 40 cells of chars.
In every legal coordinate of 5 chars "(1,2)" i got 2 chars i need to save.
so each time i can save 40/5=8 coordinates
i got 2 chars"1" "2" which i need to put.
in a row of cords[8][2]

again i can have only 2 rows filled out of the available 8.
one row per one coordinate
i know how to change a chat to integer
but i cant figure our how to arrange them in the 2d array??

i tried
Code:
```leng2=leng/5;
for(index=0;index<leng2;index++){
for(kndex=0;kndex<2;kndex++){

tr=input[index+kndex]-'0';
cords[index][kndex]=tr;
}
}```

2. Originally Posted by transgalactic2
I got a string of maximum 40 cells of chars.
In every legal coordinate of 5 chars "(1,2)" i got 2 chars i need to save.
so each time i can save 40/5=8 coordinates
i got 2 chars"1" "2" which i need to put.
in a row of cords[8][2]
if you know the input to be saved is all ints, you can scan them into corresponding integer variables.
Originally Posted by transgalactic2
again i can have only 2 rows filled out of the available 8.
one row per one coordinate
i know how to change a chat to integer
but i cant figure our how to arrange them in the 2d array??
not sure what you mean by the above

3. how divide a 1d aray into pairs?

4. Originally Posted by transgalactic2
how divide a 1d aray into pairs?
Just wanted to point out that please try to provide relevant details instead of answering a question with another.

That said, how does your input data look like; show a sample of the input along with its formatting so it can be parsed by relevant routines. If I get it, your program waits on stdin until you type something like (1,2) i.e. a pair of coordinates, one per line. Your program parses the input and puts it into a 2D array coords[8][2] (and this is where you need help?).

So in the example above the program should set coords[0][0] = 1 and coords[0][1] = 2. Is that right??

5. Originally Posted by transgalactic2
I got a string of maximum 40 cells of chars.
In every legal coordinate of 5 chars "(1,2)" i got 2 chars i need to save.
so each time i can save 40/5=8 coordinates
i got 2 chars"1" "2" which i need to put.
in a row of cords[8][2]

again i can have only 2 rows filled out of the available 8.
one row per one coordinate
i know how to change a chat to integer
but i cant figure our how to arrange them in the 2d array??

i tried
Code:
```leng2=leng/5;
for(index=0;index<leng2;index++){
for(kndex=0;kndex<2;kndex++){

tr=input[index+kndex]-'0';
cords[index][kndex]=tr;
}
}```
Well there are easier ways to parse a string.
Code:
```#include <stdio.h>

int main()
{
int coords[8][2];

char data[] = "(0,2)(1,3)(2,4)(3,5)(4,6)(5,7)(6,8)(7,9)";

char * scan = data;
int i = 0;
while ( *scan != '\0' ) {
int pos;
if ( 2 == sscanf( scan, "(%d,%d)%n", &coords[i][0], &coords[i][1], &pos ) ) {
scan += pos;
i++;
}
else {
break;
}
}

i = 0;
while (i < 8) {
printf( "point %d:\n", i );
printf( "\tx point = %d\n", coords[i][0] );
printf( "\ty point = %d\n", coords[i][1] );
i++;
}

return 0;
}```
Modify as necessary.

6. I put this working code that cuts a string into pairs and produces a 2d int array.
i changed
i put this input (in option 2) "(2, 1) ( 1, 1) ( 2, 2 ) "
i am supposed to get
2 1
1 1
2 2
array

but it prints
2 -16
-48 -48
-48 -48

I tried to change the < if the for loops into <=
its not working.
i marked the transformation part with "start pair array" and "end pair array"
??

Code:
```#include <stdio.h>

#define N 9
int is_valid(const char input[],int leng);
void mainPlayGame(board);
int playGame(char board[N][N],int size);
void printBoard(char board[N][N],int size);
int removeMine(char board[N][N],int size,int x,int y);
int unCover(char board[N][N],int size,int x,int y);

int main(){
char input[40];
char input2[40];
char board[N][N];
mainPlayGame(board);
printf("bye, please press enter to exit!\n");
getchar();
return 0;
}

void mainPlayGame(board){
int opt,leng,size=-1;
int index,kndex;
char input[40];
char ch_cords[40];
char input2[40];
int tr;
int cords[8][2];
int tndex;

int i,k,j,ch,l;

int  leng2;
do
{

scanf("%d",&opt);
l=getchar();
if (opt==1){
printf("enter board size [1..9] (no input control is needed):\n");
scanf("%d",&size);
}

if (opt==2){
if (size==-1){
printf("you must choose board size first!\n");
}
else{
printf("enter string\n"); //start pair array
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
{
input2[i] = ch;
}

input2[i] = '\0';

k=0;
for(index=0;index<=i;index++){
if(input2[index]!=' '){
input[k]=input2[index];
k++;
}
}
leng=k;
if (is_valid(input,leng))
{
leng2=leng/5;
k=0;
for(index=0;index<=leng2;index++){
if((input2[index]!='(')&&(input2[index]!=',')&&(input2[index]!=')')){
ch_cords[k]=input2[index];
k++;
}
}
j=0;
for(index=0;index<=leng2;index++){
for(kndex=0;kndex<2;kndex++){
tr=ch_cords[j]-'0';
cords[index][kndex]=tr;
j++;
}
}

for (index = 0; index < leng2; index++)
{
for (kndex = 0; kndex < 2; kndex++)
{
printf("%d ",cords[index][kndex]);
}
printf("\n");
}
}//end pair array
else//else
{
printf("%s is invalid.\n", input);
}

//////////////////////////////////////////////input check

}
}//end if opt 2
if (opt==3){
}
if (opt==4){
}
if (opt==5){
}
if (opt==0){
}
}while(opt!=0);
}

int playGame(char board[N][N],int size){

}

printf("--------------------------\n");
printf("welcome to the game\n");
printf("1. choose board size\n");
printf("2. place mines\n");
printf("3. remove mines\n");
printf("4. show mines\n");
printf("5. start the game\n");
printf("0. exit\n");

}

void printBoard(char board[N][N],int size){
int index,kndex;
for(index=0;index<size;index++){
for(kndex=0;kndex<size;kndex++){
printf("+-+");

}
printf("\n");
for(kndex=0;kndex<size;kndex++){

printf("|%c|",board[index][kndex]);
}
printf("\n");

}
for(kndex=0;kndex<size;kndex++){
printf("+-+");

}
}

int removeMine(char board[N][N],int size,int x,int y)
{
if (board[y-1][x-1]=='*'){
board[y-1][x-1]=' ';
printf("mine removed");
return 1;
}
else
{
printf("wrong input or no mine");
return 0;
}

}

int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost\n");
board[y-1][x-1]='X';
printBoard(board, size);
return 0;
}
else
{

if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}

if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
count++;
}

if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}

if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}

board[y-1][x-1]='0' +count;
printBoard(board, size);
return 1;

}
}

int is_valid(const char input[],int leng)
{
int flag=3;
int i=1;
/* Match "(<number>,<number>)" */
int index;
for(index=0;index<leng;index++){
if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')')){
if (((input[1])<'0')||((input[1])>'9')){
return 0;
}
}
}
if (input[0] != '(')
{
flag=(int)NULL;
}
if (((input[1])<'0')||((input[1])>'9')){
flag=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){

} //it wiil get char '2'

if (input[i] == ',') {   //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9')){
flag=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){

}
if (input[i]==')'){
i++;
}
else
flag=(int)NULL;
}
else{
flag=(int)NULL;
}
//////////////////////////////////////////////////////////////
if (flag ==(int)NULL)
{
return 0;
}

/* Match zero or more " (<number>,<number>)" */
while (input[i]!= '\0')
{

if (input[i] != '(')
{
flag=(int)NULL;
}
if (((input[i+1])<'0')||((input[i+1])>'9')){
flag=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){

} //it wiil get char '2'

if (input[i] == ',') {   //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9')){
flag=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){

}
if (input[i]==')'){
i++;
}
else
flag=(int)NULL;
}
else{
flag=(int)NULL;
}
if (flag ==(int)NULL)
{
return 0;
}
}

///////////////////////////////////////
return 1;
//////////////////////////////////////
}```

7. 48 is the ascii number for 0. (zero).

So your program is subtracting 0 where it should not be, in that section of code.

If you need more help on this, post back, but I believe you can find and solve this problem, with this information.

8. i changed the <= into < so it will not go "out of band"
its still gives me a similar result to this "(2, 1) ( 1, 1) ( 2, 2 ) " input

Code:
```#include <stdio.h>

#define N 9
int is_valid(const char input[],int leng);
void mainPlayGame(board);
int playGame(char board[N][N],int size);
void printBoard(char board[N][N],int size);
int removeMine(char board[N][N],int size,int x,int y);
int unCover(char board[N][N],int size,int x,int y);

int main(){
char input[40];
char input2[40];
char board[N][N];
mainPlayGame(board);
printf("bye, please press enter to exit!\n");
getchar();
return 0;
}

void mainPlayGame(board){
int opt,leng,size=-1;
int index,kndex;
char input[40];
char ch_cords[40];
char input2[40];
int tr;
int cords[8][2];
int tndex;

int i,k,j,ch,l;

int  leng2;
do
{

scanf("%d",&opt);
l=getchar();
if (opt==1){
printf("enter board size [1..9] (no input control is needed):\n");
scanf("%d",&size);
}

if (opt==2){
if (size==-1){
printf("you must choose board size first!\n");
}
else{
printf("enter string\n"); //start pair array
for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
{
input2[i] = ch;
}

input2[i] = '\0';

k=0;
for(index=0;index<=i;index++){
if(input2[index]!=' '){
input[k]=input2[index];
k++;
}
}
leng=k;
if (is_valid(input,leng))
{
leng2=leng/5;
k=0;
for(index=0;index<leng2;index++){
if((input2[index]!='(')&&(input2[index]!=',')&&(input2[index]!=')')){
ch_cords[k]=input2[index];
k++;
}
}
j=0;
for(index=0;index<leng2;index++){
for(kndex=0;kndex<2;kndex++){
tr=ch_cords[j]-'0';
cords[index][kndex]=tr;
j++;
}
}

for (index = 0; index < leng2; index++)
{
for (kndex = 0; kndex < 2; kndex++)
{
printf("%d ",cords[index][kndex]);
}
printf("\n");
}
}//end pair array
else//else
{
printf("%s is invalid.\n", input);
}

//////////////////////////////////////////////input check

}
}//end if opt 2
if (opt==3){
}
if (opt==4){
}
if (opt==5){
}
if (opt==0){
}
}while(opt!=0);
}

int playGame(char board[N][N],int size){

}

printf("--------------------------\n");
printf("welcome to the game\n");
printf("1. choose board size\n");
printf("2. place mines\n");
printf("3. remove mines\n");
printf("4. show mines\n");
printf("5. start the game\n");
printf("0. exit\n");

}

void printBoard(char board[N][N],int size){
int index,kndex;
for(index=0;index<size;index++){
for(kndex=0;kndex<size;kndex++){
printf("+-+");

}
printf("\n");
for(kndex=0;kndex<size;kndex++){

printf("|%c|",board[index][kndex]);
}
printf("\n");

}
for(kndex=0;kndex<size;kndex++){
printf("+-+");

}
}

int removeMine(char board[N][N],int size,int x,int y)
{
if (board[y-1][x-1]=='*'){
board[y-1][x-1]=' ';
printf("mine removed");
return 1;
}
else
{
printf("wrong input or no mine");
return 0;
}

}

int unCover(char board[N][N],int size,int x,int y){
int count=0;
if (board[y-1][x-1]=='*'){
printf("you lost\n");
board[y-1][x-1]='X';
printBoard(board, size);
return 0;
}
else
{

if (((y-2)>=0) && ((x-2)>=0) && (board[y-2][x-2]=='*')){
count++;
}

if (((y-2)>=0) && ((x-1)>=0) && (board[y-2][x-1]=='*')){
count++;
}
if (((y-2)>=0) && ((x)<size) && (board[y-2][x]=='*')){
count++;
}

if (((y)<size) && ((x-2)>=0) && (board[y][x-2]=='*')){
count++;
}

if (((y)<size) && ((x-1)>=0) && (board[y][x-1]=='*')){
count++;
}
if (((y)<size) && ((x)<size) && (board[y][x]=='*')){
count++;
}
if (((y-1)>=0) && ((x-2)>=0) && (board[y-1][x-2]=='*')){
count++;
}
if (((y-1)>=0) && ((x)<size) && (board[y-1][x]=='*')){
count++;
}

board[y-1][x-1]='0' +count;
printBoard(board, size);
return 1;

}
}

int is_valid(const char input[],int leng)
{
int flag=3;
int i=1;
/* Match "(<number>,<number>)" */
int index;
for(index=0;index<leng;index++){
if((input[index]!='(')&&(input[index]!=',')&&(input[index]!=')')){
if (((input[1])<'0')||((input[1])>'9')){
return 0;
}
}
}
if (input[0] != '(')
{
flag=(int)NULL;
}
if (((input[1])<'0')||((input[1])>'9')){
flag=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){

} //it wiil get char '2'

if (input[i] == ',') {   //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9')){
flag=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){

}
if (input[i]==')'){
i++;
}
else
flag=(int)NULL;
}
else{
flag=(int)NULL;
}
//////////////////////////////////////////////////////////////
if (flag ==(int)NULL)
{
return 0;
}

/* Match zero or more " (<number>,<number>)" */
while (input[i]!= '\0')
{

if (input[i] != '(')
{
flag=(int)NULL;
}
if (((input[i+1])<'0')||((input[i+1])>'9')){
flag=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){

} //it wiil get char '2'

if (input[i] == ',') {   //'2' differs ','
if (((input[i+1])<'0')||((input[i+1])>'9')){
flag=(int)NULL;
}
i++;
while((input[i]<='9')&&(input[i]>='0')){

}
if (input[i]==')'){
i++;
}
else
flag=(int)NULL;
}
else{
flag=(int)NULL;
}
if (flag ==(int)NULL)
{
return 0;
}
}

///////////////////////////////////////
return 1;
//////////////////////////////////////
}```

9. Maybe it's just too early in the morning for me, but i don't see a "start pair array" and "end pair array", in your posted code.

I do see a lot of "i++"'s in your last section of code. They don't appear to have adequate checking logic to stop them from going out of bounds of your array.

If you'll step through that part of your code, and put a watch on your input values, I believe you'll see what the problem is.

10. i got it solved .
thanks