Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

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

๐ŸŒ™