Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C program to show the implementation of array of pointers to strings...

    • 0
    • 5
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 546
    Comment on it
    /*=========================== Program to sort the strings in an array of pointers to strings =======/
    /============================ By   :Bipin Gosain ============================/	
    ============================= Date :27-5-2016	  ============================*/
    //=========================== Note : this program is written to be compiled with  c99 standard compiler ===========
    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    #define LENGTH_OF_NAME 50
    #define NO_OF_NAMES 5
    
    
    //==================== function to display the contents of the array =============================================
    void displayArrayContent(char *pointerToStrings[]){
    	for(int i = 0 ; i < NO_OF_NAMES ; i++){
    		printf("\t%s", pointerToStrings[i]);
    	}	
    	printf("\n");
    }
    //==================== function to sort the array of names ================================
    void sortNames(char *pointerToStrings[]){
    	char *temp = (char*)malloc(sizeof(char));
    	for(int i = 0 ; i < NO_OF_NAMES - 1 ; i++){
    		for(int j = 0 ; j < NO_OF_NAMES-i-1 ; j++){
    			if(strcmp(pointerToStrings[j], pointerToStrings[j+1])<0){
    				temp = pointerToStrings[j];
    				pointerToStrings[j] = pointerToStrings[j + 1];
    				pointerToStrings[j + 1] = temp;
    			}
    		}
    	}
    }
    
    void main(){
    	char *pointerToStrings[NO_OF_NAMES];// this array of pointers to string(names) can contain 10 string
    	//Allocating memory to the array of pointers
    	for(int i = 0 ; i < NO_OF_NAMES ; i++){
    		pointerToStrings[i] = (char *) malloc(sizeof(char)*LENGTH_OF_NAME);
    		printf("Enter %d string : ", (i + 1));
    		scanf("%[^\n]s", pointerToStrings[i]);
    		getchar();
    	}
    	printf("Original contents in the array are : \n");
    	displayArrayContent(pointerToStrings);
    	sortNames(pointerToStrings);
    	printf("Sorted contents in the array are : \n");
    	displayArrayContent(pointerToStrings);
    }
    
    

    Above program reads the number of multi word strings from user and then sort them in descending order alphabeticcally.

    Program is run and tested on gcc compiler with c99 standard.

    The most important point regarding this program is reading input using pointers.

    Hope you guys enjoy it.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: