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 words in a string based on their length

C Program: Sort Words in a String by Length

๐Ÿ”ท C Program: Sort Words in a String by Length

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



void sort(char words[][50], int count)
{
    char temp[50];
    for (int i = 0; i < count - 1; i++)
    {
        for (int j = i + 1; j < count; j++)
        {
            if (strlen(words[i]) > strlen(words[j]))
            {
                strcpy(temp, words[i]);
                strcpy(words[i], words[j]);
                strcpy(words[j], temp);
            }
        }
    }
}

int main()
{
    char str[200];
    char words[50][50];
    int count = 0;

    printf("Enter a string:\n");
    fgets(str, sizeof(str), stdin);

    int len = strlen(str);
    if (str[len - 1] == '\n')
    {
        str[len - 1] = '\0';
    }

    char *token = strtok(str, " ");
    while (token != NULL)
    {
        strcpy(words[count++], token);
        token = strtok(NULL, " ");
    }

    sort(words, count);

    for (int i = 0; i < count; i++)
    {
        printf("%s ", words[i]);
    }
    printf("\n");

    return 0;
}
  

๐Ÿ“˜ Explanation:

This program sorts the words in a given string **based on their lengths** using arrays and string manipulation functions in C.

๐Ÿ”น The input string is first taken using `fgets()` to safely read multi-word input.
๐Ÿ”น A newline character (`\n`) at the end is replaced with `\0` to clean the input.
๐Ÿ”น The `strtok()` function is used to **tokenize** the string by spaces into individual words, which are stored in a 2D array `words`.

๐Ÿ”น The `sort()` function uses **nested loops** to compare the length of each word using `strlen()`: - If one word is longer than the other, they are swapped using `strcpy()` and a temporary buffer.

๐Ÿ”น After sorting, the words are printed in increasing order of their lengths.

๐Ÿ” Sample Output:

Enter a string:
i love programming in C

i in C love programming
    

๐Ÿท️ Keywords:

C string sorting, C program for sorting words, strtok usage in C, sort by word length, 2D array string sort, beginner C string program

Comments

Popular Posts

๐ŸŒ™