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

C Program to Sort Strings Alphabetically and Concatenate

Sort and Concatenate Strings in C

πŸ”· C Program to Sort Strings Alphabetically and Concatenate

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

void alfa(char a[][100], int num)
{
    char temp[100];
    for(int i=0; i<num-1; i++)
    {
        for(int j=i+1; j<num; j++)
        {
            if(strcmp(a[i], a[j]) > 0)
            {
                strcpy(temp, a[i]);
                strcpy(a[i], a[j]);
                strcpy(a[j], temp);
            }
        }
    }
}

int main()
{
    char s[100][100], result[1000] = "";
    int num;
    printf("How many strings you want to enter:\n");
    scanf("%d", &num);
    
    printf("Enter %d strings:\n", num);
    for(int i=0; i<num; i++)
    {
        scanf("%s", s[i]);
    }

    alfa(s, num);

    for(int i=0; i<num; i++)
    {
        strcat(result, s[i]);
    }

    printf("Strings in alphabetical order: %s\n", result);
}
  

πŸ“˜ Explanation:

✅ This C program takes multiple strings as input from the user, sorts them in **alphabetical (lexicographical) order**, and then concatenates all the sorted strings into a single final string.

πŸ”Ή `alfa()` is a user-defined function that sorts the array of strings using **bubble sort logic** with `strcmp()` for comparison and `strcpy()` for swapping.

πŸ”Ή Inside `main()`, the user is asked for the number of strings, which are stored in a 2D character array.

πŸ”Ή After sorting, we use `strcat()` to append each string in order to the `result` string.

πŸ”Ή This is useful when you want to create a single combined string from multiple strings in a defined order (e.g., dictionary ordering).

πŸ” Sample Output:

How many strings you want to enter:
4
Enter 4 strings:
banana
apple
mango
cherry
Strings in alphabetical order:applebananacherrymango
    

🏷️ Keywords:

C program, string sorting, alphabetical string sort, strcat, strcmp, strcpy, string array in C, sort strings in C, beginner C string example

Comments

Popular Posts

πŸŒ™