473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 10659
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Starfox | last post by:
The Following Code: //Setup the options string InputName, OutputSkeletonName, OutputModelName; program_options::options_description OptionDesc("Available Options"); OptionDesc.add_options()...
3
by: sali2 | last post by:
I need someones help with the following problem. When trying to compile:"can't find symbol,symbole:constructor sam(java.lang.string,java,lang,string) location :class Mine" appears please help me....
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
5
by: blackg | last post by:
Input string not in correct format -------------------------------------------------------------------------------- I am trying to view a picture from a table. I am getting this error Input string...
7
by: Justin | last post by:
i need to build the unsigned character string: "PR0N\0Spam\0G1RLS\0Other\0Items\0\0\0" from the signed character string: "PR0N Spam G1RLS Other Items" Tokeninzing the character string is not...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
0
by: thrash423 | last post by:
ok guys this is like my last resort im already 1 day late on this project and i really cant afford to drop this class this block im in datastructers which is kickin my butt already but anyway help if...
27
by: user | last post by:
Have require file with several query stings in it. Depending on user input one of strings is selected. Everything going along smoothly until I wanted to also input a variable in string. If I put...
5
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.