plz help me write a programme that asks user to input different names and then sort them i alplabetical order
Printable View
plz help me write a programme that asks user to input different names and then sort them i alplabetical order
Have you done anything? Do you have any idea and you are stuck? You ve got to give us something to work with.
Obviously you must use a sort algorithm. Do you have something specific in mind?
How have you declared your array. Have you written something like char name[][] or have you written something like char *names[] where each pointer will show to a name?
And also
http://www.google.com/search?hl=en&q...+c&btnG=Search
i know how to sort numbers in ascending/descending order... but i am confused with strings ! i dunno how to use strings in array ?
names[row][col] is normal, where col = # of columns
Post up your trial code, and we'll assist. We won't assist otherwise. That's a necessary policy because we just become a homework service for lazy students, otherwise.
char s[100];
gets(s);
.
.
.
that is how i input string. so now s[0] is the first character in string, s[1] is second character and so on... but that is all about single string only... how can i define strings in array... a simple example could help me!
i am new to c programming, plz forgive if i sound like stupid...
So why would it be different from anything else? An array of something is declared as (something)[]. Since in this case, the something is char [], your array of strings will be char[][].
No sweat necrolord. How about an include file for standard io? We'll need that.
Code:/*
makes an array for 5 names (names[0] - names[4], which can each have up to 24 letters in their name.
The last space we'll save for the end of string char: '\0', which C uses to mark the end of any string.
*/
char names[5][25];
oh thanks adak.. so that is how i can define strings in an array... that was all i needed for now..
qsort!
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(void *a, void *b);
int main()
{
char *names[5];
char name[80];
int i, j;
printf("enter up to 5 names> ");
for (i = 0; i < 5 && gets(name); i++) names[i] = strdup(name);
qsort(names, i, sizeof(char*), cmp);
for (j = 0; j < i; j++) puts(names[j]);
return EXIT_SUCCESS;
}
int cmp(void *a, void *b)
{
return strcmp(*(char**)a, *(char**)b);
}
thx meldreth but i would like to it on my own with some help by you guys .. :D btw, what does qsort do?
I am still unable to input the string... :(Code:int i;
typedef char* string;
char name[10][50];
for(i=0;i<10;i++)
{
printf("Enter a name : ");
gets(name);
}
name is an array of strings. You should be reading into name[i]. Oh, and instead of using gets(), use fgets() so that you can specify the maximum string length.
qsort puts things in order based on the last argument. since the last argument in my example was strcmp, it puts the strings in alphabetical order.
name is a 2d array and gets expects a 1d array. change it to gets(name[i]).
Error: noname00.cpp(11,4):Illegal character '' (0x1)Code:#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int i;
typedef char* string;
char name[10][50];
for(i=0;i<10;i++)
{
printf("Enter a name : ");
fgets(name[i]);
}
printf("%s",name[2]); /* just checking if my input is working */
getch();
}
Error: noname00.cpp(13,19):Too few parameters in call to 'fgets(char *,int,FILE *)'
there's a control character right after the second opening curly brace. just clear that line and type it again.Quote:
Originally Posted by n3cr0_l0rd
fgets expect three arguments: an array to populate, the size of the array, and an input stream. try fgets(name[i], 50, stdin) instead. stdin is the stream that gets uses automatically.Quote:
Originally Posted by n3cr0_l0rd