Reading words from the keyboard and puting them in a Matrix
up vote
-1
down vote
favorite
I have to read 5 words from the keyboard and put them in a matrix. For example if I have the word RED, the letters will be split between the columns of the first row. R E D and so on.
This is my code but it exits after I scanf 5 letters
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char mat[3][3];
for(int i=0; i<2; i++)
for(int j=0;j<2;j++)
{
scanf("%s", &mat[i][j]);
}
for(int i=0; i<2; i++)
for(int j=0;j<2;j++)
{
printf("%st",mat[i][j]);
}
return 0;
}
c
add a comment |
up vote
-1
down vote
favorite
I have to read 5 words from the keyboard and put them in a matrix. For example if I have the word RED, the letters will be split between the columns of the first row. R E D and so on.
This is my code but it exits after I scanf 5 letters
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char mat[3][3];
for(int i=0; i<2; i++)
for(int j=0;j<2;j++)
{
scanf("%s", &mat[i][j]);
}
for(int i=0; i<2; i++)
for(int j=0;j<2;j++)
{
printf("%st",mat[i][j]);
}
return 0;
}
c
1
Thenscanf
won't work. Usefgetc
. And HORSE has 5 letters where your matrix only has 3 columns.
– Paul Ogilvie
Nov 11 at 11:33
@PaulOgilvie the user must enter words using the keyboard, not an external file
– Coder Disorder
Nov 11 at 11:36
How aboutfgetc(stdin)
?
– Paul Ogilvie
Nov 11 at 11:45
1
And read thescanf
documentation about the format specification because yourscanf("%s", &mat[i][j]);
has a big error causing your program to abort. Some compilers will warn you about your error, so turn warnings of your compiler on.
– Paul Ogilvie
Nov 11 at 11:47
1
There are many, many layers of abstraction between "the keyboard" and your program. You do not want to read from the keyboard. You want to read from stdin, and you need to stop thinking about stdin as "the keyboard".
– William Pursell
Nov 11 at 11:57
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have to read 5 words from the keyboard and put them in a matrix. For example if I have the word RED, the letters will be split between the columns of the first row. R E D and so on.
This is my code but it exits after I scanf 5 letters
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char mat[3][3];
for(int i=0; i<2; i++)
for(int j=0;j<2;j++)
{
scanf("%s", &mat[i][j]);
}
for(int i=0; i<2; i++)
for(int j=0;j<2;j++)
{
printf("%st",mat[i][j]);
}
return 0;
}
c
I have to read 5 words from the keyboard and put them in a matrix. For example if I have the word RED, the letters will be split between the columns of the first row. R E D and so on.
This is my code but it exits after I scanf 5 letters
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
char mat[3][3];
for(int i=0; i<2; i++)
for(int j=0;j<2;j++)
{
scanf("%s", &mat[i][j]);
}
for(int i=0; i<2; i++)
for(int j=0;j<2;j++)
{
printf("%st",mat[i][j]);
}
return 0;
}
c
c
edited Nov 11 at 11:36
asked Nov 11 at 11:31
Coder Disorder
72
72
1
Thenscanf
won't work. Usefgetc
. And HORSE has 5 letters where your matrix only has 3 columns.
– Paul Ogilvie
Nov 11 at 11:33
@PaulOgilvie the user must enter words using the keyboard, not an external file
– Coder Disorder
Nov 11 at 11:36
How aboutfgetc(stdin)
?
– Paul Ogilvie
Nov 11 at 11:45
1
And read thescanf
documentation about the format specification because yourscanf("%s", &mat[i][j]);
has a big error causing your program to abort. Some compilers will warn you about your error, so turn warnings of your compiler on.
– Paul Ogilvie
Nov 11 at 11:47
1
There are many, many layers of abstraction between "the keyboard" and your program. You do not want to read from the keyboard. You want to read from stdin, and you need to stop thinking about stdin as "the keyboard".
– William Pursell
Nov 11 at 11:57
add a comment |
1
Thenscanf
won't work. Usefgetc
. And HORSE has 5 letters where your matrix only has 3 columns.
– Paul Ogilvie
Nov 11 at 11:33
@PaulOgilvie the user must enter words using the keyboard, not an external file
– Coder Disorder
Nov 11 at 11:36
How aboutfgetc(stdin)
?
– Paul Ogilvie
Nov 11 at 11:45
1
And read thescanf
documentation about the format specification because yourscanf("%s", &mat[i][j]);
has a big error causing your program to abort. Some compilers will warn you about your error, so turn warnings of your compiler on.
– Paul Ogilvie
Nov 11 at 11:47
1
There are many, many layers of abstraction between "the keyboard" and your program. You do not want to read from the keyboard. You want to read from stdin, and you need to stop thinking about stdin as "the keyboard".
– William Pursell
Nov 11 at 11:57
1
1
Then
scanf
won't work. Use fgetc
. And HORSE has 5 letters where your matrix only has 3 columns.– Paul Ogilvie
Nov 11 at 11:33
Then
scanf
won't work. Use fgetc
. And HORSE has 5 letters where your matrix only has 3 columns.– Paul Ogilvie
Nov 11 at 11:33
@PaulOgilvie the user must enter words using the keyboard, not an external file
– Coder Disorder
Nov 11 at 11:36
@PaulOgilvie the user must enter words using the keyboard, not an external file
– Coder Disorder
Nov 11 at 11:36
How about
fgetc(stdin)
?– Paul Ogilvie
Nov 11 at 11:45
How about
fgetc(stdin)
?– Paul Ogilvie
Nov 11 at 11:45
1
1
And read the
scanf
documentation about the format specification because your scanf("%s", &mat[i][j]);
has a big error causing your program to abort. Some compilers will warn you about your error, so turn warnings of your compiler on.– Paul Ogilvie
Nov 11 at 11:47
And read the
scanf
documentation about the format specification because your scanf("%s", &mat[i][j]);
has a big error causing your program to abort. Some compilers will warn you about your error, so turn warnings of your compiler on.– Paul Ogilvie
Nov 11 at 11:47
1
1
There are many, many layers of abstraction between "the keyboard" and your program. You do not want to read from the keyboard. You want to read from stdin, and you need to stop thinking about stdin as "the keyboard".
– William Pursell
Nov 11 at 11:57
There are many, many layers of abstraction between "the keyboard" and your program. You do not want to read from the keyboard. You want to read from stdin, and you need to stop thinking about stdin as "the keyboard".
– William Pursell
Nov 11 at 11:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Since you haven't specified any size for the strings... I will presume they are of arbitrary length...
// Takes input using the 'stdin' stream...
char* read_input(void)
{
char ch;
size_t len = 0;
size_t size = len + 2;
char* str = realloc(NULL, size);
if (!str)
return str;
while ((ch = fgetc(stdin)) != -1 && ch != 'n')
{
str[len++] = ch;
if (len == size)
{
str = realloc(str, size += 2);
if (!str)
return str;
}
}
str[len++] = '';
return realloc(str, len);
}
This function will read the input, now we also need a function for checking if the string is a valid word... i.e, it contains only alphabets...
// Checks whether the specified string is alphabetic or not...
int is_alpha_string(char* str, char* err_msg)
{
for (unsigned i = 0u; i < strlen(str); i++)
if (!isalpha(str[i]))
{
fprintf(stderr, err_msg);
return 0;
}
return 1;
}
After this, just do:
// The 'main()' function...
int main(void)
{
char* matrix[5];
for (unsigned i = 0u; i < 5u; i++)
{
printf("Enter your word here: ");
matrix[i] = read_input();
i -= !is_alpha_string(matrix[i], "Error! Entered text is not a valid word!nn");
}
for (int i = 0; i < 5; i++)
printf("%sn", matrix[i]);
return 0;
}
Edit: And don't forget to add these includes at the top:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
str = realloc(str, size += 2);
will leak the memstr
pointed to before ifrealloc()
fails. Also,return realloc(str, len);
is a no-no.
– Swordfish
Nov 11 at 21:28
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Since you haven't specified any size for the strings... I will presume they are of arbitrary length...
// Takes input using the 'stdin' stream...
char* read_input(void)
{
char ch;
size_t len = 0;
size_t size = len + 2;
char* str = realloc(NULL, size);
if (!str)
return str;
while ((ch = fgetc(stdin)) != -1 && ch != 'n')
{
str[len++] = ch;
if (len == size)
{
str = realloc(str, size += 2);
if (!str)
return str;
}
}
str[len++] = '';
return realloc(str, len);
}
This function will read the input, now we also need a function for checking if the string is a valid word... i.e, it contains only alphabets...
// Checks whether the specified string is alphabetic or not...
int is_alpha_string(char* str, char* err_msg)
{
for (unsigned i = 0u; i < strlen(str); i++)
if (!isalpha(str[i]))
{
fprintf(stderr, err_msg);
return 0;
}
return 1;
}
After this, just do:
// The 'main()' function...
int main(void)
{
char* matrix[5];
for (unsigned i = 0u; i < 5u; i++)
{
printf("Enter your word here: ");
matrix[i] = read_input();
i -= !is_alpha_string(matrix[i], "Error! Entered text is not a valid word!nn");
}
for (int i = 0; i < 5; i++)
printf("%sn", matrix[i]);
return 0;
}
Edit: And don't forget to add these includes at the top:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
str = realloc(str, size += 2);
will leak the memstr
pointed to before ifrealloc()
fails. Also,return realloc(str, len);
is a no-no.
– Swordfish
Nov 11 at 21:28
add a comment |
up vote
0
down vote
Since you haven't specified any size for the strings... I will presume they are of arbitrary length...
// Takes input using the 'stdin' stream...
char* read_input(void)
{
char ch;
size_t len = 0;
size_t size = len + 2;
char* str = realloc(NULL, size);
if (!str)
return str;
while ((ch = fgetc(stdin)) != -1 && ch != 'n')
{
str[len++] = ch;
if (len == size)
{
str = realloc(str, size += 2);
if (!str)
return str;
}
}
str[len++] = '';
return realloc(str, len);
}
This function will read the input, now we also need a function for checking if the string is a valid word... i.e, it contains only alphabets...
// Checks whether the specified string is alphabetic or not...
int is_alpha_string(char* str, char* err_msg)
{
for (unsigned i = 0u; i < strlen(str); i++)
if (!isalpha(str[i]))
{
fprintf(stderr, err_msg);
return 0;
}
return 1;
}
After this, just do:
// The 'main()' function...
int main(void)
{
char* matrix[5];
for (unsigned i = 0u; i < 5u; i++)
{
printf("Enter your word here: ");
matrix[i] = read_input();
i -= !is_alpha_string(matrix[i], "Error! Entered text is not a valid word!nn");
}
for (int i = 0; i < 5; i++)
printf("%sn", matrix[i]);
return 0;
}
Edit: And don't forget to add these includes at the top:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
str = realloc(str, size += 2);
will leak the memstr
pointed to before ifrealloc()
fails. Also,return realloc(str, len);
is a no-no.
– Swordfish
Nov 11 at 21:28
add a comment |
up vote
0
down vote
up vote
0
down vote
Since you haven't specified any size for the strings... I will presume they are of arbitrary length...
// Takes input using the 'stdin' stream...
char* read_input(void)
{
char ch;
size_t len = 0;
size_t size = len + 2;
char* str = realloc(NULL, size);
if (!str)
return str;
while ((ch = fgetc(stdin)) != -1 && ch != 'n')
{
str[len++] = ch;
if (len == size)
{
str = realloc(str, size += 2);
if (!str)
return str;
}
}
str[len++] = '';
return realloc(str, len);
}
This function will read the input, now we also need a function for checking if the string is a valid word... i.e, it contains only alphabets...
// Checks whether the specified string is alphabetic or not...
int is_alpha_string(char* str, char* err_msg)
{
for (unsigned i = 0u; i < strlen(str); i++)
if (!isalpha(str[i]))
{
fprintf(stderr, err_msg);
return 0;
}
return 1;
}
After this, just do:
// The 'main()' function...
int main(void)
{
char* matrix[5];
for (unsigned i = 0u; i < 5u; i++)
{
printf("Enter your word here: ");
matrix[i] = read_input();
i -= !is_alpha_string(matrix[i], "Error! Entered text is not a valid word!nn");
}
for (int i = 0; i < 5; i++)
printf("%sn", matrix[i]);
return 0;
}
Edit: And don't forget to add these includes at the top:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
Since you haven't specified any size for the strings... I will presume they are of arbitrary length...
// Takes input using the 'stdin' stream...
char* read_input(void)
{
char ch;
size_t len = 0;
size_t size = len + 2;
char* str = realloc(NULL, size);
if (!str)
return str;
while ((ch = fgetc(stdin)) != -1 && ch != 'n')
{
str[len++] = ch;
if (len == size)
{
str = realloc(str, size += 2);
if (!str)
return str;
}
}
str[len++] = '';
return realloc(str, len);
}
This function will read the input, now we also need a function for checking if the string is a valid word... i.e, it contains only alphabets...
// Checks whether the specified string is alphabetic or not...
int is_alpha_string(char* str, char* err_msg)
{
for (unsigned i = 0u; i < strlen(str); i++)
if (!isalpha(str[i]))
{
fprintf(stderr, err_msg);
return 0;
}
return 1;
}
After this, just do:
// The 'main()' function...
int main(void)
{
char* matrix[5];
for (unsigned i = 0u; i < 5u; i++)
{
printf("Enter your word here: ");
matrix[i] = read_input();
i -= !is_alpha_string(matrix[i], "Error! Entered text is not a valid word!nn");
}
for (int i = 0; i < 5; i++)
printf("%sn", matrix[i]);
return 0;
}
Edit: And don't forget to add these includes at the top:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
edited Nov 11 at 12:57
answered Nov 11 at 12:51
Ruks
658111
658111
str = realloc(str, size += 2);
will leak the memstr
pointed to before ifrealloc()
fails. Also,return realloc(str, len);
is a no-no.
– Swordfish
Nov 11 at 21:28
add a comment |
str = realloc(str, size += 2);
will leak the memstr
pointed to before ifrealloc()
fails. Also,return realloc(str, len);
is a no-no.
– Swordfish
Nov 11 at 21:28
str = realloc(str, size += 2);
will leak the mem str
pointed to before if realloc()
fails. Also, return realloc(str, len);
is a no-no.– Swordfish
Nov 11 at 21:28
str = realloc(str, size += 2);
will leak the mem str
pointed to before if realloc()
fails. Also, return realloc(str, len);
is a no-no.– Swordfish
Nov 11 at 21:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248291%2freading-words-from-the-keyboard-and-puting-them-in-a-matrix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Then
scanf
won't work. Usefgetc
. And HORSE has 5 letters where your matrix only has 3 columns.– Paul Ogilvie
Nov 11 at 11:33
@PaulOgilvie the user must enter words using the keyboard, not an external file
– Coder Disorder
Nov 11 at 11:36
How about
fgetc(stdin)
?– Paul Ogilvie
Nov 11 at 11:45
1
And read the
scanf
documentation about the format specification because yourscanf("%s", &mat[i][j]);
has a big error causing your program to abort. Some compilers will warn you about your error, so turn warnings of your compiler on.– Paul Ogilvie
Nov 11 at 11:47
1
There are many, many layers of abstraction between "the keyboard" and your program. You do not want to read from the keyboard. You want to read from stdin, and you need to stop thinking about stdin as "the keyboard".
– William Pursell
Nov 11 at 11:57