Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

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

๐ŸŒ™