Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

๐ŸŒ™