Hello,
Though I have done this question without string functions but can anybody give me short algorithm for this. Here is the question:
Write a program that reverse the words in a sentence:
Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: you can't cage a swallow can you?

Here is the Hint I've found in the book:
Use a loop to read a character one by one and store them in an array. Have the loop stop at a period, question mark, or exclamation point( the "terminating character"), which is saved in a separate char variable. Then use a second loop to search backward through the array for the beginning of the last word. Print the last word, then search backward for the next-to-last word. Repeat until the beginning of the array is reached. Finally, print the terminating character.

This program must be done without <string.h> header functions, no gets(), no puts(), only getchar() or scanf() functions.
Here is my solution:

 #include <stdio.h>

#define N 100

int main(void) {
  char ch[N], terminate;
  int i, j, k, len;
  printf("Enter a sentence: ");

  for(i = 0; i < N; i++) {
    ch[i] = getchar();
    if(ch[i] == '?' || ch[i] == '.' || ch[i] == '!' || ch[i] == '\n') {
      len = i;        //Determine the length of the string
      terminate = ch[i];
      break;
    }
  }

  int word_count = 0;
  for(j = len - 1; j >= 0; j--) {
    word_count++;
    if(ch[j] == ' ') {
      for(k = j+1; k < len; k++) {
        printf("%c", ch[k]);
      }
      printf("%c", ch[j]);  // print space character
      len = len - word_count;
      word_count = 0;
    }

  }

  for(i = 0; i < len; i++) {
    printf("%c", ch[i]);
    if(ch[i] == ' ') {
      break;
    }
  }

  printf("%c", terminate);

  return 0;
}

Enter a sentence: you can cage a swallow can't you?
Reversal of sentence: you can't cage a swallow can you?

I'm assuming you meant the reversal to be "you can't swallow a cage can you?", because the one you posted would require a bit more work in specifying which words are being reversed as opposed to reversing all of them.

And the "hint" actually tells you the whole algorithm:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    char s[] = "you can cage a swallow can't you";
    size_t begin = 0, end;
    size_t i;

    puts(s);

    /* Reverse the whole string */
    _strrev(s);

    /* Reverse each word in the string */
    while (s[begin]) {
        end = begin;

        /* Find the end of the word */
        while (s[end] && !isspace(s[end])) {
            ++end;
        }

        /* Reverse the word */
        for (i = end - 1; begin < i; begin++, i--) {
            char temp = s[i];
            s[i] = s[begin];
            s[begin] = temp;
        }

        /* Find the beginning of the next word */
        begin = end;

        while (s[begin] && isspace(s[begin])) {
            ++begin;
        }
    }

    puts(s);

    return 0;
}

And before you cry that I didn't comply with the restrictions of the assignment, it's not my assignment to do, so you can use my code as a template but not turn it in for a grade.

Hey, I already mention that the question must be done without string.h header function. I can also do this by your way, but I want to do this without string.h header function.
And by the way, my solution is right in all the way. If you have any solution without the use <string.h> header function, then provide it. Also this is not a homework.

Hey, I already mention that the question must be done without string.h header function.

And I'm not going to do your homework for you.

I can also do this by your way, but I want to do this without string.h header function.

The only thing I used from string.h is the non-portable strrev() function, which is super easy to write. You can even learn how to write it by looking at the code I gave you. What I'm getting from your response is that you didn't bother to read the whole of my post and refuse to take similar code and learn from it to write code better suited to your specific problem.

In other words, why should I bother helping further when you don't seem interested in helping yourself?

Also this is not a homework.

It's an assignment of some sort that you've been given or taken upon yourself to complete. Therefore, it's homework under Daniweb's definition of the word.

hey,
First thing I mention that this is not a homework and I don't want to use any string method, size_t because this question is from the book where I have to do this by using array, getchar(), putchar(), scanf() or printf(), loops only which I've already done it. I just asked that is there is any shorter version of this using only loops and without string.h and ctype.h header.
The answer you have mention is easily found on the web and answer I've done is only one done by me.

And by the way I'm a self learner not a university student.

I just asked that is there is any shorter version of this using only loops and without string.h and ctype.h header.

No. And you should probably stop thinking of 40 lines as "long", because that's teeny tiny from my perspective.

And by the way I'm a self learner not a university student.

That's completely irrelevant.

This is a question from chapter-8 of K.N. King book where no strings are covered and I have to do this only by arrays and loops not by string functions like you.

I know string method is better than mine and it's easy. But this is not a way of learning. I have to become more comfortable with arrays and loops first then I'll go to next chapters.

I can make these 40 lines as 4k lines but short algorithm makes code better.
I think you can't solve this question by another method without strings. That's why you gave these stupid comments.

King book where no strings are covered and I have to do this only by arrays and loops not by string functions like you.

Can you not read? I already stated that the only string function I used was strrev(). I also essentially duplicated the effect of strrev() later in the code without using any other string functions. You can replace size_t with int, the strrev() call with a facsimile and there will be nothing from the string.h header used in that code. It's mostly a manual solution, though to be honest that's only because strrev() isn't suitable for reversing substrings.

I can't tell if you're really as stupid as you're presenting yourself, or just pissed off that I'm forcing you to use your brain and do actual work.

I think you can't solve this question by another method without strings.

If you mean without string functions provided by the standard library then I'll direct you to exhibit A, where I've written all of those functions. I'm in no way dependent on the standard library. If it's not there, I'll just write it myself (and I have). Can you do the same?

So you might consider that just because someone refuses to do your homework for you, it doesn't mean they're unable to do it.

See your code, you have usedisspace(), gets(), and puts() functions also, which I don't allowed.

I don't need your answer because you don't know how to do without strings functions. Now I made my code slightly lesser by decreasing one loop.

#include <stdio.h>

#define N 100

int main(void) {
  char ch[N], terminate;
  int i, j, k, len;
  printf("Enter a sentence: ");
  for(i = 0; i < N; i++) {
    ch[i] = getchar();
    if(ch[i] == '?' || ch[i] == '.' || ch[i] == '!' || ch[i] == '\n') {
      len = i;        //Determine the length of the string
      terminate = ch[i];
      break;
    }
  }
  for(j = len - 1; j >= -1; j--) {
    if(j < 0 || ch[j] == ' ') {
      for(k = j+1; k < len; k++) 
        printf("%c", ch[k]);

      if (j>=0) {
        printf("%c",ch[j]);
        len = j;
      }
    }

  }
  printf("%c", terminate);

  return 0;
}

That's what I asking to you.

commented: Moron. I'm done here. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.