![]() |
| ||
| sorting characters in a string... help!!!! help.. i dont know how to sort strings.. for example.. if i enter: JOKER then the descending order of string must appear: ROKJE i think of strcpy() ..but it i dont know what to place in the if().. here's my code.. #include<iostream.h> how could i implement the strcpy or the strcmp in my code?? |
| ||
| Re: sorting characters in a string... help!!!! Here is an example to implement strcmp(): int strcmp ( const char * str1, const char * str2 ); <cstring> Compare two strings Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached. Parameters str1 C string to be compared. str2 C string to be compared. Return Value Returns an integral value indicating the relationship between the strings: A zero value indicates that both strings are equal. A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite. Example /* strcmp example */ #include <stdio.h> #include <string.h> int main () { char szKey[] = "apple"; char szInput[80]; do { printf ("Guess my favourite fruit? "); gets (szInput); } while (strcmp (szKey,szInput) != 0); puts ("Correct answer!"); return 0; } Output: Guess my favourite fruit? orange Guess my favourite fruit? apple Correct answer! |
| ||
| Re: sorting characters in a string... help!!!! Look, you have that #include<string.h>right? So why don't you use c++ strings? It's like string str;and it's easier. Now this
is a mess. You could just compare a[c] < a[c+1] cuz this are characters, not strings and they are already sorted in ASCII. Could you please explain what the program is supposed to do? |
| ||
| Re: sorting characters in a string... help!!!! Quote:
|
| ||
| Re: sorting characters in a string... help!!!! Introducing bubble sort :) #include <iostream>This works. |
| ||
| Re: sorting characters in a string... help!!!! THANK YOU VERY MUCH!!! IT WORKS!! but can you please explain to me how this code works.? |
| ||
| Re: sorting characters in a string... help!!!! Well, i just transformed your own code. Used string strinstead of char* a[100]and char tmpinstead of char* tmp[100](u need just one character to bubble-sort, not an array). You would like to use strings in c++ instead of char arrays in cases like this, because it's much easier to do so. Like you can write string1 = string2 instead of strcpy. that's all |
| ||
| Re: sorting characters in a string... help!!!! Thanks sa penguin!!! |
| ||
| Re: sorting characters in a string... help!!!! help here again... if i enter AaBbCc the output should be CcBbAa or cCbBaA.. but the output appears cbaCBA.. help!!! heres the code.. #include<iostream.h> |
| ||
| Re: sorting characters in a string... help!!!! Quote:
<operator uses the ASCII table. You need to use toupperor tolowerin the line above to convert the digits from upper to lower case or vice versa and handle the comparison. http://www.cplusplus.com/reference/c...e/toupper.html http://www.cplusplus.com/reference/c...e/tolower.html |
| ||
| Re: sorting characters in a string... help!!!! Quote:
C++ files don't have .h, so it's <iostream>. Important to remember, since <string.h> is very different from <string>. Also try C++'s string with built in functions of convenience. Place your sorting method in a function to clear things up a tad. Use newlines to space out parts of your code. main() must return zero. clrscr(), is that portable? Now as Vernon recommended, just need to compare it in one case then flip it back keeping track of it with a boolean statement. |
| ||
| Re: sorting characters in a string... help!!!! or In #5 in the code you can change line 14 from if(str[i] < str[j])to if(toupper(str[i]) < toupper(str[j])). |
| ||
| Re: sorting characters in a string... help!!!! Thanks for all the help! :d |
| All times are GMT -4. The time now is 4:16 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC