• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

strlen()

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does this code print 4 instead of 14 when I type "this is a test"? isn't it supposed to give the length of the string?

 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put some debugging in. Change line 12 to something like this:-I suggest you search for details of scanf(). It is only scanning the first token of your text and stopping at the space.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link about scanf().
Link about reading the whole of a line with scanf().
 
Ranch Hand
Posts: 179
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Link about reading the whole of a line with scanf().


I would suggest using fgets(), as mentioned in that link.
 
Hanna Roberts
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where does it talk about fgets()?
 
Hanna Roberts
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh nvm I found the section
 
Hanna Roberts
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how come now its deleting the first word in the sentence?

 
John Matthews
Ranch Hand
Posts: 179
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fgets() should be used instead of scanf() to read the input. In your code scanf() is still reading ('deleting') the first word, and fgets() is reading the remainder. Try removing the scanf().
 
Hanna Roberts
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh ok thanks, Ill try that
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic