You are here:

C/String program

Advertisement


Question
I need to write a program using scanf() that reads in X number of strings and then sorts them alphabetically. I understand how to do everything except how to store X number of lines. I thought of using a loop function that declares X number of strings but am unable to do that. I also thought of writing the strings to a seperate file then pulling them back and sorting them. So far I have not been able to get any tangible results. Please help. I am using "Microsoft Visual C++ .NET"

Answer
You can try char **str;

You can use it like this:

char **str;
int i;

str = (char **) malloc(100);
for (i = 0; i < 100; i++) str[i] = (char *) malloc(256);

In the above example, length of each string is 256 characters.
And you can store 100 strings in str.
Each of str[i] is a string for you.

Hope this solves your problem.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.