Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

Reverse Word Order in C

Reverse Word Order in C

✅ Reverse Word Order in C

#include <stdio.h>

int main() {
    char str[200];           // To store the full input string
    char words[50][50];      // 2D array to store individual words
    int i = 0, j = 0, k = 0; // i: index in str, j: index in words, k: word count

    // Input the full line (including spaces)
    printf("Enter a string: ");
    scanf(" %[^\n]", str);  // Read input until newline

    // Split the string into words
    while (str[i] != '\0') {
        if (str[i] != ' ') {
            words[k][j++] = str[i];  // Add character to current word
        } else {
            words[k][j] = '\0';  // Terminate current word
            k++;                 // Move to next word
            j = 0;               // Reset letter index
        }
        i++;
    }
    words[k][j] = '\0';  // Terminate last word

    // Print words in reverse order
    printf("Reversed word order:\n");
    for (int l = k; l >= 0; l--) {
        printf("%s ", words[l]);
    }
    printf("\n");

    return 0;
}
  

πŸ“˜ Explanation:

This C program reads a full string input (including spaces), splits it into individual words using space as a delimiter, and then prints the words in reverse order.

It uses a 2D character array to store each word separately. The user input is captured using scanf(" %[^\n]", str), which allows spaces in the input.

Finally, the program prints each word starting from the last to the first, effectively reversing the sentence word-by-word.

🧾 Sample Output:

Enter a string: 
Welcome to C Programming

Reversed word order:
Programming C to Welcome
  

πŸ”‘ Keywords:

Reverse Word Order in C, C String Manipulation, String with spaces in scanf, C Program for reversing sentence, Reverse sentence C, Array of strings in C, Split and reverse in C

πŸ“Œ Hashtags:

#CProgramming #StringManipulation #ReverseWords #CLanguage #BeginnerC #StringReversal

Comments

Popular Posts

πŸŒ™