Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

๐ŸŒ™