Blog Archive

Sunday, August 29, 2010

Program to sort list of names using using pointers.

/*
Program to sort list of names using using pointers.
*/
#include<conio.h>
#include<stdio.h>

char upper(char);

void main()
{
char *names[10];
char name[20],*temp;
int i=0,i2=0;
while(i<10)
{
printf("Enter your name.");
names[i] = (char *)malloc(20);
scanf("%s",names[i]);
i++;
}
for(i=0; i<10; i++)
{
printf("%s\n",names[i]);
}
//strcpy(names[0],);
printf("\n\n");
for(i=0; i<10;i++)
{
for(i2=i+1;i2<10; i2++)
{
if(upper(names[i2][0]) {
temp = names[i];
names[i] = names[i2];
names[i2] = temp;
}
}
}

for(i=0; i<10; i++)
{
printf("%s\n",names[i]);
}

}

char upper(char c)
{

if(c<'a')
return c;
else
c -=32;
return c;

}

No comments:

Post a Comment