Using scanf for particular inputs
#include<stdio.h>
int main(void)
{
char z[10];
scanf("%[aeiou]",z);
printf("%s",z);
return 0;
}
I am trying to enter a string and want the output which includes a,e,i,o,u only as specified in scanf
function. I want to try getting the output using only scanf
.
c scanf
add a comment |
#include<stdio.h>
int main(void)
{
char z[10];
scanf("%[aeiou]",z);
printf("%s",z);
return 0;
}
I am trying to enter a string and want the output which includes a,e,i,o,u only as specified in scanf
function. I want to try getting the output using only scanf
.
c scanf
1
Your code works for me. What's your issue?
– interjay
Jan 19 at 14:18
2
If you are trying to read a string containing various characters and printed only the vowels from it, thenscanf
with"%[aeiou]"
is not what you want. That tellsscanf
to read only a, e, i, o, and u, and to stop when it sees anything else. You would want to continue to read the whole string. So, if this is what you want, you should read the entire string, then print only the vowels. (You can read the entire string character-by-character; you do not need to read it all at once.)
– Eric Postpischil
Jan 19 at 14:39
1
You are right. I was trying to read only the vowels and then print that, but I wasn’t sure on how to do that.
– vaibbhav devender
Jan 19 at 14:53
It is possible to do this with scanf and loop, but you'd really want to process the input yourself character by character and check if each of them is a vowel or not.
– Antti Haapala
Jan 19 at 15:39
add a comment |
#include<stdio.h>
int main(void)
{
char z[10];
scanf("%[aeiou]",z);
printf("%s",z);
return 0;
}
I am trying to enter a string and want the output which includes a,e,i,o,u only as specified in scanf
function. I want to try getting the output using only scanf
.
c scanf
#include<stdio.h>
int main(void)
{
char z[10];
scanf("%[aeiou]",z);
printf("%s",z);
return 0;
}
I am trying to enter a string and want the output which includes a,e,i,o,u only as specified in scanf
function. I want to try getting the output using only scanf
.
c scanf
c scanf
edited Jan 19 at 14:19
Mohammadreza Farahani
2,06131424
2,06131424
asked Jan 19 at 14:13
vaibbhav devendervaibbhav devender
12
12
1
Your code works for me. What's your issue?
– interjay
Jan 19 at 14:18
2
If you are trying to read a string containing various characters and printed only the vowels from it, thenscanf
with"%[aeiou]"
is not what you want. That tellsscanf
to read only a, e, i, o, and u, and to stop when it sees anything else. You would want to continue to read the whole string. So, if this is what you want, you should read the entire string, then print only the vowels. (You can read the entire string character-by-character; you do not need to read it all at once.)
– Eric Postpischil
Jan 19 at 14:39
1
You are right. I was trying to read only the vowels and then print that, but I wasn’t sure on how to do that.
– vaibbhav devender
Jan 19 at 14:53
It is possible to do this with scanf and loop, but you'd really want to process the input yourself character by character and check if each of them is a vowel or not.
– Antti Haapala
Jan 19 at 15:39
add a comment |
1
Your code works for me. What's your issue?
– interjay
Jan 19 at 14:18
2
If you are trying to read a string containing various characters and printed only the vowels from it, thenscanf
with"%[aeiou]"
is not what you want. That tellsscanf
to read only a, e, i, o, and u, and to stop when it sees anything else. You would want to continue to read the whole string. So, if this is what you want, you should read the entire string, then print only the vowels. (You can read the entire string character-by-character; you do not need to read it all at once.)
– Eric Postpischil
Jan 19 at 14:39
1
You are right. I was trying to read only the vowels and then print that, but I wasn’t sure on how to do that.
– vaibbhav devender
Jan 19 at 14:53
It is possible to do this with scanf and loop, but you'd really want to process the input yourself character by character and check if each of them is a vowel or not.
– Antti Haapala
Jan 19 at 15:39
1
1
Your code works for me. What's your issue?
– interjay
Jan 19 at 14:18
Your code works for me. What's your issue?
– interjay
Jan 19 at 14:18
2
2
If you are trying to read a string containing various characters and printed only the vowels from it, then
scanf
with "%[aeiou]"
is not what you want. That tells scanf
to read only a, e, i, o, and u, and to stop when it sees anything else. You would want to continue to read the whole string. So, if this is what you want, you should read the entire string, then print only the vowels. (You can read the entire string character-by-character; you do not need to read it all at once.)– Eric Postpischil
Jan 19 at 14:39
If you are trying to read a string containing various characters and printed only the vowels from it, then
scanf
with "%[aeiou]"
is not what you want. That tells scanf
to read only a, e, i, o, and u, and to stop when it sees anything else. You would want to continue to read the whole string. So, if this is what you want, you should read the entire string, then print only the vowels. (You can read the entire string character-by-character; you do not need to read it all at once.)– Eric Postpischil
Jan 19 at 14:39
1
1
You are right. I was trying to read only the vowels and then print that, but I wasn’t sure on how to do that.
– vaibbhav devender
Jan 19 at 14:53
You are right. I was trying to read only the vowels and then print that, but I wasn’t sure on how to do that.
– vaibbhav devender
Jan 19 at 14:53
It is possible to do this with scanf and loop, but you'd really want to process the input yourself character by character and check if each of them is a vowel or not.
– Antti Haapala
Jan 19 at 15:39
It is possible to do this with scanf and loop, but you'd really want to process the input yourself character by character and check if each of them is a vowel or not.
– Antti Haapala
Jan 19 at 15:39
add a comment |
1 Answer
1
active
oldest
votes
1) Avoid a width-less string format such as "%[aeiou]"
- it is prone to overflow.
2) Check scanf()
return value.
To avoid limitations such as 10, read input in a loop.
int main(void) {
char z[10];
while (scanf("%9[aeiou]",z) == 1) {
printf("%s",z);
}
printf("n");
return 0;
}
If code needs to read input like xxxaxxxaxxx
and output aa
only with scanf()
, more work is needed.
To be clear scanf()
is not the best tool to use to accomplish this goal.
Some untested idea of what is needed.
int main(void) {
do {
int cnt;
char z[10];
while ((cnt = scanf("%9[aeiou]",z)) == 1) {
printf("%s",z);
}
if (cnt == EOF) {
break;
}
// Find the character that stopped scanning
if ((cnt = scanf("%1[^aeiou]",z)) == 1) {
if (z[0] == 'n') {
break;
}
} else {
break;
}
}
printf("n");
return 0;
}
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f54267967%2fusing-scanf-for-particular-inputs%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
1) Avoid a width-less string format such as "%[aeiou]"
- it is prone to overflow.
2) Check scanf()
return value.
To avoid limitations such as 10, read input in a loop.
int main(void) {
char z[10];
while (scanf("%9[aeiou]",z) == 1) {
printf("%s",z);
}
printf("n");
return 0;
}
If code needs to read input like xxxaxxxaxxx
and output aa
only with scanf()
, more work is needed.
To be clear scanf()
is not the best tool to use to accomplish this goal.
Some untested idea of what is needed.
int main(void) {
do {
int cnt;
char z[10];
while ((cnt = scanf("%9[aeiou]",z)) == 1) {
printf("%s",z);
}
if (cnt == EOF) {
break;
}
// Find the character that stopped scanning
if ((cnt = scanf("%1[^aeiou]",z)) == 1) {
if (z[0] == 'n') {
break;
}
} else {
break;
}
}
printf("n");
return 0;
}
add a comment |
1) Avoid a width-less string format such as "%[aeiou]"
- it is prone to overflow.
2) Check scanf()
return value.
To avoid limitations such as 10, read input in a loop.
int main(void) {
char z[10];
while (scanf("%9[aeiou]",z) == 1) {
printf("%s",z);
}
printf("n");
return 0;
}
If code needs to read input like xxxaxxxaxxx
and output aa
only with scanf()
, more work is needed.
To be clear scanf()
is not the best tool to use to accomplish this goal.
Some untested idea of what is needed.
int main(void) {
do {
int cnt;
char z[10];
while ((cnt = scanf("%9[aeiou]",z)) == 1) {
printf("%s",z);
}
if (cnt == EOF) {
break;
}
// Find the character that stopped scanning
if ((cnt = scanf("%1[^aeiou]",z)) == 1) {
if (z[0] == 'n') {
break;
}
} else {
break;
}
}
printf("n");
return 0;
}
add a comment |
1) Avoid a width-less string format such as "%[aeiou]"
- it is prone to overflow.
2) Check scanf()
return value.
To avoid limitations such as 10, read input in a loop.
int main(void) {
char z[10];
while (scanf("%9[aeiou]",z) == 1) {
printf("%s",z);
}
printf("n");
return 0;
}
If code needs to read input like xxxaxxxaxxx
and output aa
only with scanf()
, more work is needed.
To be clear scanf()
is not the best tool to use to accomplish this goal.
Some untested idea of what is needed.
int main(void) {
do {
int cnt;
char z[10];
while ((cnt = scanf("%9[aeiou]",z)) == 1) {
printf("%s",z);
}
if (cnt == EOF) {
break;
}
// Find the character that stopped scanning
if ((cnt = scanf("%1[^aeiou]",z)) == 1) {
if (z[0] == 'n') {
break;
}
} else {
break;
}
}
printf("n");
return 0;
}
1) Avoid a width-less string format such as "%[aeiou]"
- it is prone to overflow.
2) Check scanf()
return value.
To avoid limitations such as 10, read input in a loop.
int main(void) {
char z[10];
while (scanf("%9[aeiou]",z) == 1) {
printf("%s",z);
}
printf("n");
return 0;
}
If code needs to read input like xxxaxxxaxxx
and output aa
only with scanf()
, more work is needed.
To be clear scanf()
is not the best tool to use to accomplish this goal.
Some untested idea of what is needed.
int main(void) {
do {
int cnt;
char z[10];
while ((cnt = scanf("%9[aeiou]",z)) == 1) {
printf("%s",z);
}
if (cnt == EOF) {
break;
}
// Find the character that stopped scanning
if ((cnt = scanf("%1[^aeiou]",z)) == 1) {
if (z[0] == 'n') {
break;
}
} else {
break;
}
}
printf("n");
return 0;
}
edited Jan 19 at 16:10
answered Jan 19 at 15:55
chuxchux
82.6k872149
82.6k872149
add a comment |
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.
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%2f54267967%2fusing-scanf-for-particular-inputs%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
Your code works for me. What's your issue?
– interjay
Jan 19 at 14:18
2
If you are trying to read a string containing various characters and printed only the vowels from it, then
scanf
with"%[aeiou]"
is not what you want. That tellsscanf
to read only a, e, i, o, and u, and to stop when it sees anything else. You would want to continue to read the whole string. So, if this is what you want, you should read the entire string, then print only the vowels. (You can read the entire string character-by-character; you do not need to read it all at once.)– Eric Postpischil
Jan 19 at 14:39
1
You are right. I was trying to read only the vowels and then print that, but I wasn’t sure on how to do that.
– vaibbhav devender
Jan 19 at 14:53
It is possible to do this with scanf and loop, but you'd really want to process the input yourself character by character and check if each of them is a vowel or not.
– Antti Haapala
Jan 19 at 15:39