0

I'm trying to write a program that reads (accepts) a sentence with spaces until the user ends it with a point. for some reason, my code stops when the user types two points instead of only one, I'll leave my code below:

char sent[100];
int i;    

printf("Write a sentence and end it with a point :\n");

for (i=0; sent[i]!='.'; i++)
{
    scanf("%s",&sent[i]);
}
10
  • 3
    Can you explain what you think this loop does? for (i=0; sent[i]!='.'; i++) Commented Mar 30, 2021 at 1:17
  • 4
    You're testing sent[i] before you fill it in. Commented Mar 30, 2021 at 1:20
  • 1
    %s reads a whole word, not a single character. Commented Mar 30, 2021 at 1:21
  • 1
    Your use of &sent[i] makes very little sense. If the user types abc defg. it's going to write abc into char[0] through char[2], then it will write defg into char[1] through char[4], overwriting part of abc. Commented Mar 30, 2021 at 1:23
  • 1
    Use a debugger. Commented Mar 30, 2021 at 1:24

1 Answer 1

2

This code will match the first 99 non-period characters and save them in sent.

char sent[100];

scanf("%99[^.]", sent);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.