0

So I am taking in a text file like so:

Hello. This is my test! I need to print sentences.
Can I do it? Any help is welcomed. Thanks!

And I am trying for output:

1. Hello.
2. This is my test!
3. I need to print sentences.
4. Can I do it?
...and so on....

Here is what I have written so far:

#include <stdio.h>
#include <stdlib.h>
  main() {
     int storage[50];
     int i = 0 ;
     int linecount = 1 ;
     char c;

     for (;;) {
      c=getchar();
      if(c == '\n'){
        c == '\0';}
      storage[i] = c;
      i++;

      if (c == '.' || c == '!' || c == '?') {
        int j ;
        printf("%d. ", linecount++);
        for (j = 0; j < i; j++) {
         printf("%c", storage[j]); }
        i = 0 ;
        printf("\n");
      }
   }
 }

And it results with output:

1. Hello.
2.  This is my test!
3.  I need to print sentences.
4. 
Can I do it?
5.  Any help is welcomed.
6.  Thanks!

My first issue is that it prints a '\n' for line 4, I thought that my code:

if(c == '\n'){
  c == '\0';}

Would take care of this, but that is not the case.

My second issue is that it adds an extra space char after the first record. I know this is because I use the print statement:

"%d. "

and also the beginning of the sentences have a space to separate from last sentence but I am unsure on how to fix this issue. Any help would be great! Thanks! -qorz

qorz
  • 49
  • 1
  • 7
  • Duplicate of [this SO question.](http://stackoverflow.com/questions/22354843/c-reading-and-identifying-sentences/22355557) – Jabberwocky Mar 12 '14 at 17:35

3 Answers3

0

Your first issue occurs because you need to use = operator, not ==. You are actually testing the value instead of changing the array position to \0.

To fix this, change

if(c == '\n'){
    c == '\0';
}

to

if(c == '\n'){
    c = '\0';
}

For your second question, take a look here.

Community
  • 1
  • 1
Mauren
  • 1,955
  • 2
  • 18
  • 28
0

Here == is not assignment operator, change it to =

if(c == '\n'){
   c = '\0'; 
}
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
0
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main() {
    int storage[50];
    int i = 0 ;
    int linecount = 1 ;
    int c;

    while(EOF!=(c=getchar())) {
        storage[i++] = c;
        if (c == '.' || c == '!' || c == '?') {
            int j ;
            printf("%d. ", linecount++);
            for (j = 0; j < i; j++) {
                printf("%c", storage[j]);
            }
            printf("\n");
            i = 0 ;
            while(isspace(c=getchar()))
                ;//skip white spaces
            ungetc(c, stdin);
        }
    }

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70