Search
 
SCRIPT & CODE EXAMPLE
 

C

sort names in array in c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size_of_array 3  //define size of the array
 
//function to display array
void display(char array[][30]){
  for(int i=0; i<size_of_array; i++){
    printf("%s ", array[i]);
  }
  printf("
");
}
 
int main()
{
  //create an array of strings
  char array[size_of_array][30];

  //Inputting names
  printf("Enter %d Strings: 
", size_of_array);
  for(int i=0; i<size_of_array; i++){
    scanf("%s", array[i]);
  }

  //display the original array
  printf("Original array: ");
  display(array);

  char temp[30];

  //Sort array using the Buuble Sort algorithm
  for(int i=0; i<size_of_array; i++){
    for(int j=0; j<size_of_array-1-i; j++){
      if(strcmp(array[j], array[j+1]) > 0){
        //swap array[j] and array[j+1]
        strcpy(temp, array[j]);
        strcpy(array[j], array[j+1]);
        strcpy(array[j+1], temp);
      }
    }
  }

  //display the sorted array
  printf("Sorted Array: ");
  display(array);

  return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: bubble sort in c 
C :: Bitwise Operators in C language 
C :: linked list using c 
C :: c remove last charachter from string 
C :: number pattern in c 
C :: array of strings in c 
C :: mongo connect db 
C :: function array median 
C :: print an int c 
C :: sockaddr_in c 
C :: Initialization of a 3d array in c 
C :: lxc Failed to load config for 
C :: c check if character is upper case 
C :: Program to print all palindromes in a given range 
C :: tuples in c 
C :: bool c++ 
C :: commenting in c 
C :: c calling a function 
C :: owasp 
C :: C/AL Convertion of Decimal to String/Text 
C :: solutionadda 
C :: left me on read 
C :: check if a number is even and bigger than or equal to 16 using bitwise 
C :: C access global variable same name 
C :: table de hachage en c 
C :: python project script run 
C :: Chef in Vaccination Queue codechef solution in c++ 
C :: c joystick arduino 
C :: c# Regex similar wor 
C :: largest value in u32 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =