C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Problem Statement:
  • Problem Explanation:
  • Algorithm for Sorting Strings using Bubble Sort:
  • Pseudocode:
  • C Program:
  • Program Explanation:

Was this helpful?

  1. Strings

Program: Bubble Sort

Problem Statement:

Write a C program that sorts an array of strings using the Bubble Sort algorithm. The program should take the following steps:

  1. Input:

    • Prompt the user to input the number of strings (n).

    • Allow the user to input each string.

  2. Bubble Sort:

    • Implement the Bubble Sort algorithm to sort the array of strings lexicographically.

    • Use the strcmp function to compare strings and the strcpy function to swap strings during the sorting process.

  3. Output:

    • Display the sorted strings in ascending order.

  4. Example:

    • If the user inputs:

      Input number of strings: 3
      Input string 1: zero
      Input string 2: one
      Input string 3: two
    • The program should output:

      The strings appear after sorting:
      one
      two
      zero

Problem Explanation:

The program utilizes the Bubble Sort algorithm, a simple sorting technique, to arrange an array of strings. It compares adjacent strings using strcmp and swaps them if they are out of order. This process is repeated until the array is sorted. The resulting sorted strings are then displayed to the user.

Algorithm for Sorting Strings using Bubble Sort:

  1. Input the number of strings n.

  2. Create an array strings to store the strings.

  3. Input each string into the array.

  4. Implement the Bubble Sort algorithm using strcmp and strcpy functions to compare and swap strings.

  5. Display the sorted strings.

Pseudocode:

Function swapStrings(str1, str2):
    temp = str1
    str1 = str2
    str2 = temp

Function bubbleSortStrings(strings[], n):
    for i from 0 to n-1:
        for j from 0 to n-i-1:
            if strcmp(strings[j], strings[j+1]) > 0:
                swapStrings(strings[j], strings[j+1])

Function main():
    Input n
    Create an array strings[MAX_STRINGS][MAX_STRING_LENGTH]
    for i from 0 to n-1:
        Input strings[i]
    
    bubbleSortStrings(strings, n)
    
    Display "The strings appear after sorting:"
    for i from 0 to n-1:
        Display strings[i]

C Program:

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

#define MAX_STRINGS 100
#define MAX_STRING_LENGTH 50

void swapStrings(char *str1, char *str2) {
    char temp[MAX_STRING_LENGTH];
    strcpy(temp, str1);
    strcpy(str1, str2);
    strcpy(str2, temp);
}

void bubbleSortStrings(char strings[][MAX_STRING_LENGTH], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (strcmp(strings[j], strings[j + 1]) > 0) {
                swapStrings(strings[j], strings[j + 1]);
            }
        }
    }
}

int main() {
    int n;

    printf("Input number of strings: ");
    scanf("%d", &n);

    if (n <= 0 || n > MAX_STRINGS) {
        printf("Invalid number of strings. Please provide a valid number.\n");
        return 1; // Return with an error code
    }

    char strings[MAX_STRINGS][MAX_STRING_LENGTH];

    for (int i = 0; i < n; i++) {
        printf("Input string %d: ", i + 1);
        scanf("%s", strings[i]);
    }

    bubbleSortStrings(strings, n);

    printf("\nThe strings appear after sorting:\n");
    for (int i = 0; i < n; i++) {
        printf("%s\n", strings[i]);
    }

    return 0;
}

Program Explanation:

This program takes the number of strings as input, reads the strings, and then uses the Bubble Sort algorithm to sort them in lexicographical order. The strcmp function is used to compare strings, and the strcpy function is used to swap strings during sorting.

If you have any questions or need further clarification, feel free to ask!

PreviousCommon String FunctionsNextSearching, Tokenizing, and Analyzing Strings

Was this helpful?