473,394 Members | 1,671 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,394 software developers and data experts.

in the printf the program remove all the strings after the space why???

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void num(char arr[100])
  3. {
  4. int i=0, count=1;
  5. while (arr[i]!='\0')
  6. {
  7. if (arr[i]==' ')
  8. {count++;}
  9. i++;
  10. }
  11. printf("%d",count);
  12. }
  13. int main()
  14. {
  15.  
  16. char data [100];
  17.  
  18. scanf("%s",&data);
  19. printf("%s",data);
  20. num(data);
  21. return 0;
  22. }
try to inter programming language
in the printf in the main it will printf programming only and remove the rest
Dec 7 '10 #1
9 3190
Banfa
9,065 Expert Mod 8TB
No that is not printf getting it wrong, that is scanf behaving exactly as specified.

scanf uses any white space as a delimiter so if you ask scanf for a string and enter "programming language" it will use the space as a delimiter and return "programming" which printf then correctly outputs.

If you want to get a whole line including spaces using the end of line as a delimiter then use the function fgets.
Dec 7 '10 #2
how can u give me an example
Dec 7 '10 #3
Banfa
9,065 Expert Mod 8TB
Perhaps you could try typing "fgets" into your browser search engine?
Dec 7 '10 #4
okai thanks
Dec 7 '10 #5
donbock
2,426 Expert 2GB
Better than typing 'fgets' into the browser ...
  1. Look for "bytes > c/c++ > c/c++ questions" in small print near the top of this browser page (just above the thread title).
  2. Click on "c/c++ questions".
  3. Then click on the first question: "PLEASE READ FIRST: Useful Links and Posting Guidelines"
  4. Scroll down to the second message in this thread. It contains Important Links.
  5. Click on either of the "C and C++ library reference" links.
  6. Now search for fgets.
Dec 7 '10 #6
when i replace the space between the words by tab it didn't work in a right way
any help in this !!
Dec 11 '10 #7
Banfa
9,065 Expert Mod 8TB
There is no reason why it would work any differently, tab is also a white space character.
Dec 13 '10 #8
donbock
2,426 Expert 2GB
Refer to reply #2. Use fgets. Don't use scanf.
Dec 13 '10 #9
Options to get rid of current problem:
gets() function instead of scanf [although this is often frowned upon]
fgets() function instead of scanf

or continue using scanf but clear the buffer with a while loop.
Google: clean buffer after scanf for more details on this.
Dec 17 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: qwweeeit | last post by:
For a python code I am writing I need to remove all strings definitions from source and substitute them with a place-holder. To make clearer: line 45 sVar="this is the string assigned to sVar"...
7
by: wk | last post by:
Hi, I have a seemingly simple html code which has two tables one under another, and both have an image of same size...but the table row also has some text, which seems to be creating a small gap...
4
by: Aaron | last post by:
is there a way to remove all white spaces in the html output? ie. everything as one line.
3
by: Paul | last post by:
Hi, My RichTextBox has multiple lines of text. Most of the lines unfortunately end with a space. Is it possible to replace the space and NewLine/Line Feed with just the NewLine/LineFeed? So...
7
by: JohnBirch | last post by:
I am new to CSS and have spent many unsuccessful hours trying to find a way to remove the space above the beginning of a paragraph. What I want to do is to have a bold heading <H3> in 12pt followed...
1
by: ThunderMusic | last post by:
Hi, I want to make a toolbox form (like the one in Adobe Photoshop), so I would like to put many buttons one beside the others (beside, over and under, just like the toolbox in photoshop). I...
4
by: Magnus Warker | last post by:
Hello group, I am producing a form within a table: <table> <tr> <td> <form> ... </form>
5
tharden3
by: tharden3 | last post by:
How do I remove white space around an icon? I have been using some helpful icon pics from google images to spice up a website that I'm making. In many instances though, the icon is not square, but...
3
by: DavidPr | last post by:
I'm trying to replace all the white space in a user submitted variable. For instance: $page_name = $_POST; needs to be changed to - SpecialOffer or better yet - specialoffer I've tried to use...
1
by: Man4ish | last post by:
Hi, I am working on data formatting. I need to format the data based on input var length by keeping some space. for example..... void Formater(string str) { int len = str.size(); ...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.