Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

Reverse a String in C

Reverse String in C

Reverse a String in C

This C program reverses a given string using a simple two-pointer technique. It swaps characters from both ends until the middle is reached.

✅ C Program Code:


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

void reverse(char str[])
{
    int start = 0;
    int end = strlen(str) - 1;
    int temp;
    while (start < end)
    {
        temp = str[start];
        str[start] = str[end];
        str[end] = temp;
        start++;
        end--;
    }
}

int main()
{
    char str[100];
    printf("Enter the string:\n");
    scanf("%[^\n]", str);
    printf("Before reversing the string: %s\n", str);
    reverse(str);
    printf("After reversing the string: %s", str);
}
  

πŸ“Œ How It Works:

  • reverse(): Swaps characters from start and end using a while loop until the full string is reversed.
  • scanf("%[^\n]", str): Reads a line of input including spaces.
  • main(): Takes user input, prints original and reversed string.

πŸ’» Sample Output:

Enter the string:
Hello World
Before reversing the string: Hello World
After reversing the string: dlroW olleH

πŸ” Keywords:

reverse string in C, string handling in C, two-pointer approach C, beginner string program, C logic examples, character array reverse

Comments

πŸŒ™