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;
}









share|improve this question




















  • 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










  • @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 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




    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















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;
}









share|improve this question




















  • 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










  • @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 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




    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













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;
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 11:36

























asked Nov 11 at 11:31









Coder Disorder

72




72








  • 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










  • @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 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




    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




    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










  • How about fgetc(stdin)?
    – Paul Ogilvie
    Nov 11 at 11:45






  • 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






  • 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












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>





share|improve this answer























  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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>





share|improve this answer























  • 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

















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>





share|improve this answer























  • 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















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>





share|improve this answer














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>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 12:57

























answered Nov 11 at 12:51









Ruks

658111




658111












  • 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


















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python