Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:00:04+00:00 2026-06-01T21:00:04+00:00

Code: #include <stdio.h> int main(void) { char i[50]; while(scanf(%s , i)){ printf(You’ve written: %s

  • 0

Code:

#include <stdio.h>
int main(void) {
    char i[50];
    while(scanf("%s ", i)){
        printf("You've written: %s \n", i);
    }
    printf("you have finished writing\n");

    return 0;
}

One problem is that the code doesn’t do as it is expected to. If I typed in:

abc def ghi.

It would output:

You've written: abc
You've written: def

How can I fix it? The goal is to read every single word from stdin until it reaches “ENTER” or a “.” (dot).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:00:06+00:00Added an answer on June 1, 2026 at 9:00 pm

    @cnicutar is pretty close, but you apparently only want to start reading at something other than white-space, and want to stop reading a single word when you get to whitespace, so for you scanset, you probably want something more like:

    while(scanf(" %49[^ \t.\n]%*c", i)) {
    

    In this, the initial space skips across any leading white space. The scan-set then reads until it gets to a space, tab, new-line or period. The %*c then reads (but throws away) the next character (normally the one that stopped the scan).

    This can, however, throw away a character when/if you reach the end of the buffer, so you may want to use %c, and supply a character to read into instead. That will let you recover from a single word longer than the buffer you supplied.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
Here is my code!(sorry for my poor english) #include<stdio.h> int convert(char ch); int main(void)
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
I got this C code. #include <stdio.h> int main(void) { int n, d, i;
Code #include<stdio.h> int main() { int i; printf(%d \n,'\1'); printf(%d \n,'\022'); printf(%d ,'\555'); return
I have a very simple piece of code: #include <stdio.h> #include <glib.h> int main(int
CODE: #include <stdio.h> main() { int nums[100], i; char answer; int count = 0;
This part of my code works fine: #include <stdio.h> int main(){ //char somestring[3] =
#include <stdio.h> #include <ctype.h> #define STRING_LEN 500 void stripspaces(char, char, char); int main(void) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.