C Examples

WAP in C to print a string in reverse order as per word

The Program in C to print a string in reverse order as per word is given below:

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

void reverse_string(char* start, char* end) {
    while (start < end) {
        char temp = *start;
        *start++ = *end;
        *end-- = temp;
    }
}

void reverse_words(char* str) {
    char* start = str;
    char* end = str;
    while (*end) {
        end++;
        if (*end == ' ') {
            reverse_string(start, end - 1);
            start = end + 1;
        }
    }
    reverse_string(start, end - 1);
    reverse_string(str, end - 1);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%[^\n]", str);
    reverse_words(str);
    printf("Reversed string: %s\n", str);
    return 0;
}

Output:

Enter a string: you are legendary goat
Reversed string: goat legendary are you

Pro-Tips💡

This program uses two helper functions, reverse_string and reverse_words, to reverse the string by word.

The reverse_string function takes two pointers as arguments, representing the start and end of a substring, and reverses the characters within that substring.

The reverse_words function uses two pointers, start and end, to iterate through the string and reverse each word.

It also reverses the entire string at the end to get the final result.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X