469,608 Members | 1,508 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,608 developers. It's quick & easy.

string input help

Hi,

I want to input a string from command line. I use the following
program to input the string.

#include<stdio.h>

int main(){
char input[100];

printf("Enter the string: ");
scanf("%s",input);
printf("The string that you entered is: %s\n",input);

return 1;
}

When my input string is: iiiiiiiiiiiiiiiii
it prints out the same string.

However, when my input string input is: iiiii iiiiiiiiiiiiiiiiiii
ot prints out only the first part of the string, that is, iiiii

I do not see any flaw in my code but I should be missing something.
Do you see anything wrong in the code?

Thanks,
Reet

Feb 13 '07 #1
4 10429
"priyanka" <pr**********@gmail.comwrites:
scanf("%s",input);
[...]
When my input string is: iiiiiiiiiiiiiiiii
it prints out the same string.

However, when my input string input is: iiiii iiiiiiiiiiiiiiiiiii
ot prints out only the first part of the string, that is, iiiii
%s stops reading at a space.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Feb 13 '07 #2
priyanka said:
Hi,

I want to input a string from command line. I use the following
program to input the string.

#include<stdio.h>

int main(){
char input[100];

printf("Enter the string: ");
scanf("%s",input);
Don't use unadorned %s with scanf - at the very least, specify how much
data you can store. In your case, it's 99 (because of the null
character requirement), so you'd want:

if(1 == scanf("%99s", input))
printf("The string that you entered is: %s\n",input);

return 1;
}

When my input string is: iiiiiiiiiiiiiiiii
it prints out the same string.

However, when my input string input is: iiiii iiiiiiiiiiiiiiiiiii
ot prints out only the first part of the string, that is, iiiii
That's right - it's reading up to the first whitespace character. Use
fgets instead.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 14 '07 #3
"priyanka" <priyankab...@gmail.comwrote:
Hi,

I want to input a string from command line. I use the following
program to input the string.

#include<stdio.h>

int main(){
char input[100];

printf("Enter the string: ");
fflush(stdout);
scanf("%s",input);
Check the return value and avoid buffer overflow...

r = scanf("%99s", input);
if (r != 1) return EXIT_FAILURE;
printf("The string that you entered is: %s\n",input);

return 1;
1 is not a portable value to pass (implicitly) to exit().
Use 0, EXIT_SUCCESS or EXIT_FAILURE. The latter macros
are defined in <stdlib.h>.
}

When my input string is: iiiiiiiiiiiiiiiii
it prints out the same string.

However, when my input string input is: iiiii iiiiiiiiiiiiiiiiiii
ot prints out only the first part of the string, that is, iiiii

I do not see any flaw in my code but I should be missing something.
You're missing the fact that programming isn't a guessing game.
Before you use a function read up on its specs. It's forgivable
to have a misunderstanding, but it's not generally forgivable to
call functions without reading up on how they work.
Do you see anything wrong in the code?
Yes, but what you're also missing is that %s will, after ignoring
leading whitespace, read non whitespace characters into the supplied
buffer, stopping at the next whitespace character (or end of file,
or when the length limit is reached.)

--
Peter

Feb 14 '07 #4
priyanka wrote:
>
Hi,

I want to input a string from command line. I use the following
program to input the string.

#include<stdio.h>

int main(){
char input[100];

printf("Enter the string: ");
scanf("%s",input);
printf("The string that you entered is: %s\n",input);

return 1;
}

When my input string is: iiiiiiiiiiiiiiiii
it prints out the same string.

However, when my input string input is: iiiii iiiiiiiiiiiiiiiiiii
ot prints out only the first part of the string, that is, iiiii

I do not see any flaw in my code but I should be missing something.
Do you see anything wrong in the code?
/* BEGIN fscanf_input.c */
/*
** There are only three different values
** that can be assigned to rc
** from the fscanf calls in this program.
** They are:
** EOF
** 0
** 1
** If rc equals EOF, then the end of file was reached,
** or there is some input problem;
** ferror and feof can be used to distinguish which.
** If rc equals 0, then an empty line was entered
** and the array contains garbage values.
** If rc equals 1, then there is a string in the array.
** Up to LENGTH number of characters are read
** from a line of a text stream
** and written to a string in an array.
** The newline character in the text line is replaced
** by a null character in the array.
** If the line is longer than LENGTH,
** then the extra characters are discarded.
*/
#include <stdio.h>

#define LENGTH 40
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1];

puts("The LENGTH macro is " xstr(LENGTH));
do {
fputs("Enter any line of text to continue,\n"
"or just hit the Enter key to quit:", stdout);
fflush(stdout);
rc = fscanf(stdin, "%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getc(stdin);
}
if (rc == 0) {
array[0] = '\0';
}
if (rc == EOF) {
puts("rc equals EOF");
} else {
printf("rc is %d. Your string is:%s\n\n", rc, array);
}
} while (rc == 1);
return 0;
}

/* END fscanf_input.c */

--
pete
Feb 14 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by sali2 | last post: by
29 posts views Thread by zoro | last post: by
5 posts views Thread by blackg | last post: by
7 posts views Thread by Justin | last post: by
13 posts views Thread by Freaker85 | last post: by
reply views Thread by thrash423 | last post: by
27 posts views Thread by user | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.