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 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

Popular Posts

πŸŒ™