Write a program that reads a string s and copy s into a string t (of the same size) in reverse order, except that a sequence of white space is squeezed to a single space.


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define size 200

int main()
{
    char s[size], t[size];
    int i, j, L;
    
    printf("Enter a string: ");
    gets(s);
    
    for(L=0; s[L]; L++);
    
    j=0;
    for(i=L-1; i>=0; i--)
         if(s[i]!=' ' || j==0 ||t[j-1]!=' ')
            t[j++]=s[i]; 
 
    t[j]='\0';
    
    printf("Output = \"%s\"\n",t);

    return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Length of the longest ascending sequence of characters