Someperson
Someperson

Reputation: 11

C program that reads a string

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]);
}

Upvotes: 0

Views: 75

Answers (1)

Ray Hamel
Ray Hamel

Reputation: 1309

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

char sent[100];

scanf("%99[^.]", sent);

Upvotes: 2

Related Questions