![]() |
|
#1
|
|||
|
|||
sorting string w/strcmpI am working on another project for school, I need to sort and print a new list.
I have to use the strcmp to help with the sorting and use #define strings 7. CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
|
Quote:
The first thing you need to do is read in a bunch of strings to sort. Read them into an array of char*'s. Get that done first, and output the strings. Next, create the sort function and pass the address of this array to the function. From there you should have it... __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
|||
|
|||
|
Quote:
This is better wouldn't you say? Now I need to go to the Next. MAyB or Mayb NoT CPP / C++ / C Code:
|
|
#4
|
|||
|
|||
|
Hi,
First you need to understand what a string means in C. In C strings are nothing but collection of chars or simply a character array. So your following declaration CPP / C++ / C Code:
str[0]='J' str[1]='o' str[2]='h' str[3]='n' str[4]='\0' here '\0' marks the end of the string. Many library functions (strlen() for e.g.)that operate on strings (char arrays), use this special character (also called as NULL) to mark the end of the string. So you need to make sure you always allocate one extra character space in your char array for NULL terminator. Else your string reading function may fail or give unexpected results. Fortunately, to make our life easier when you read in a string using either scanf(),fscanf() or sscanf() using "%s" operator, a NULL terminator is automatically added at the end of the string. Now coming back to your program. You need to compare strings and not each characters in the strings. First of all you need to define storage to store your strings i.e. char arrays. CPP / C++ / C Code:
You have defined a 7 storage locations to store the strings, each of the size "SIZE". Next you need to read in few string from user input. here is one example. CPP / C++ / C Code:
Now that you have your 7 strings you need to start comparing them using strcmp() function which takes 2 strings, string1 and string2, as arguments and returns less than 0, equal to 0 or greater than 0 if string1 is less than,equal to or greater than string2. you can use for loop to sort the string as follows CPP / C++ / C Code:
Now you have sorted all the strings in ascending order. Now all you have to do is print it. CPP / C++ / C Code:
Following is what your program output will look like. Quote:
I hope its clear enough to understand. Good luck. |
|
#5
|
||||
|
||||
|
nkhambal's explanation is very good. A little too complete to my way of thinking
Quote:
Then you put zeros in that string (line 13/14) which is unnecessary You the start a block (15) read 1 string (16/17) and end the block (18). So you read one and only one string You now print each character of the string entered (20-23) and start a loop that does nothing but change j from your current loop counter+1 to 7 (24) To define multiple strings, you need CPP / C++ / C Code:
Also, most here discourage the use of scanf() and recommend fgets() because of the flaky way scanf() works. It requires an extra line of programming but it will not mess up your input stram like scanf() does. CPP / C++ / C Code:
1) if the last character ([i]strlen(str) - 1) of the string str 2) is a return '\n' 3) change it to NULL '\0' The easiest way to find out if you did it correctly is to actually run the program. You'll get your answer faster than posting here and waiting hours for a response. Then if the code doesn't work, you can try other things and get closer to an answer (hopefully) the post when you're hopelessly stuck and we'll help pull you out of the mire __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Read a .html file, check that file for links | salemite | C Programming Language | 10 | 17-Jan-2008 08:56 |
| Re: Conversion: Binary, Decimal, Hexadecimal | WaltP | C Programming Language | 1 | 10-May-2006 07:49 |
| Including Maps and strings?? | maddie | C++ Forum | 17 | 05-Jul-2004 07:25 |
| storing a token pointer as a string | CoreLEx | C Programming Language | 1 | 07-Oct-2003 12:33 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The