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() { ...

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

πŸŒ™