Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Program to swap elements of array of pointers to strings

Posted By: Adelinda Fischer     Category: C Programming     Views: 3755

Program to swap elements of array of pointers to strings.

Code for Program to swap elements of array of pointers to strings in C Programming

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

#define MAX 6

char *names[MAX] ;
int count ;

int add ( char * ) ;
void swap ( int, int ) ;
void show( ) ;

void main( )
{
    int flag ;

    clrscr( ) ;

    flag = add ( "akshay" ) ;
    if ( flag == 0 )
        printf ( "Unable to add string" ) ;

    flag = add ( "parag" ) ;
    if ( flag == 0 )
        printf ( "Unable to add string" ) ;

    flag = add ( "raman" ) ;
    if ( flag == 0 )
        printf ( "Unable to add string" ) ;

    flag = add ( "srinivas" ) ;
    if ( flag == 0 )
        printf ( "Unable to add string" ) ;

    flag = add ( "gopal" ) ;
    if ( flag == 0 )
        printf ( "Unable to add string" ) ;

    flag = add ( "rajesh" ) ;
    if ( flag == 0 )
        printf ( "Unable to add string" ) ;
    printf ( "\nNames before swapping:\n" ) ;
    show ( ) ;

    swap ( 2, 3 ) ;
    printf ( "\nNames after swapping:\n" ) ;
    show ( ) ;

    getch( ) ;
}

/* adds given string */
int add ( char *s ) { if ( count < MAX ) { names[count] = s ; count++ ; return 1 ; } elsereturn 0 ; } /* swaps the names at given two positions */
void swap ( int i, int j ) { char *temp ; temp = names[i] ; names[i] = names[j] ; names[j] = temp ; } /* displays the elements */
void show ( ) { int i ; for ( i = 0 ; i < count ; i++ ) puts ( names[i] ) ; }
  
Share: 


Didn't find what you were looking for? Find more on Program to swap elements of array of pointers to strings Or get search suggestion and latest updates.

Adelinda Fischer
Adelinda Fischer author of Program to swap elements of array of pointers to strings is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!