-1

I am trying to write program, which take text and seperate it into sentences.
Input:

Hi, my name is John.

Output:

Hi,

my

name

is

John.

Code

int main ()
{
  int str[200]; 
  int i = 0;
  puts ("Enter text. Do not forget to put dot at the end.");
  do {
    str[i] = getchar();
    i++;
  } while (str[i-1] != '.');

  printf("\n");
  int k, lenght = 0; //lenght -- the lenght of single word

  for (i=0; str[i] != '.'; i++) {
    if (str[i] == ' ' || str[i] == '.') {
    printf ("\n");
    k = i - lenght;

    do {
        putchar (str[k]);
        k++;
    } while (str[k] != ' ');
    lenght = 0;
    }

    lenght++;
  }

  printf ("\n stop");
  return 0;
}

If you try to run or if you can see, there is an error. It does not output last word.

I tried to put there this cycle:

  do  {
    if (str[i] == ' ') {
    printf("\n");
    k=i-lenght;
    do {
        putchar(str[k]);
        k++;

    }while(str[k] != ' ');

    lenght=0;

    }

    lenght++;
    i++;
  }while(str[i+1] != '.');

But its the same cycle... I also tried to make function:

void word (char *c,int index, int lenght ) {
   printf ("\n");
   int i = index - lenght;
    do {
        putchar (c[i]);
        i++;
    } while (c[i] != ' ');
    return;
}

and I called it instead of do-while cycle (in the "if section " of the code):

for (i=0; str[i] != '.'; i++) {
    if (str[i] == ' ' || str[i] == '.') {
    word(str, i, lenght);
    lenght = 0;
    }

    lenght++;
  }

What surpriced me was, that the function was "outputting" only the firs Word in sentence. If the first word was "John" it output "John" "ohn" "hn".

So there is not just one question...

How to remake/repaire the cycle/function to output what I want - all of the words in the sentence?

Why it does not work? Well I know the answer - beacouse thy cycle is built to end on character ' ', but not the '.', but when I tried to change it, it output one more random character after dot.

Just please dont blame me for the code, I am just begginer trying to learn something. I know its not a Masterpiece and I can and I will make it shorter before I finish it.

Tehryn
  • 57
  • 1
  • 10
  • 1
    Please format your code properly. – Iharob Al Asimi Sep 27 '15 at 21:40
  • 1
    See this https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c – Steven Walton Sep 27 '15 at 21:40
  • Is there a reason for you to use '.' instead of '\0' as the phrase terminator? This will neve stop if you don't have a '.' in the end. Just a hint. – hugos Sep 27 '15 at 21:44
  • @HugoSadok I tryied it and failed... I will have to use it, I know... But first, I want to find out this problem. – Tehryn Sep 27 '15 at 21:45
  • @StevenWalton wow, thanks I will take a look at it – Tehryn Sep 27 '15 at 21:48
  • As soon as it sees `'.'`, the loop ends and the code inside the loop that prints the accumulated word never gets executed. You might want to change your loop to terminate on a similar condition that will be true only *after* a `'.'` is seen and processed. – Mark Plotnick Sep 27 '15 at 22:02
  • I believe your problem is in the for loop. You are expecting to have another iteration when str[i] == '.'. But it actually exits the loop without evaluating the last word. – hugos Sep 27 '15 at 22:09
  • Ops that's what Mark said. – hugos Sep 27 '15 at 22:10

2 Answers2

2

The reason it doesn't print the last word is that, as soon as it reads it and finds the '.', the for-loop terminates so it doesn't process and output that word.

You could change the for-loop condition to look for the terminating '\0' instead, that should fix it.

Ian
  • 1,221
  • 1
  • 18
  • 30
  • Change the check from '.' to '\0' should fix it. But is still quite ugly if you think about it. But I agree that considering the code this is probably the easiest way to fix it. – hugos Sep 27 '15 at 22:13
  • It only prints fritst letter in the first word. – Tehryn Sep 28 '15 at 07:52
  • @Tehryn I was referring to the first version of the code only – Ian Sep 28 '15 at 08:21
  • @well I know... When I change it, it prints whole sentence, but evan with NULL character, or at least some random character right after the dot. But if I put there: `if (k – Tehryn Sep 28 '15 at 08:54
1
#include<stdio.h>
int main ()
{
    char str[200];
    int i = 0;
    puts ("Enter text:");

    gets(str);

    int k, length = 0; 
    printf("So the words are:\n");
    while(str[i]!='\0')
    {

        if (str[i] == ' ') {
            k = i - length;

            do {
                putchar (str[k]);
                k++;
            } while (str[k] != ' ');
            printf ("\n");
            length = (-1);
        }
        else if (str[i+1] == '\0') {
            k = i - length;

            do {
                putchar (str[k]);
                k++;
            } while (str[k] != '\0');
            length = 0;
        }

        length++;
        i++;
    }
    return 0;
}
Asim Dinda
  • 13
  • 4