943,723 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3063
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 1st, 2009
0

Re: sorting characters in a string... help!!!!

Click to Expand / Collapse  Quote originally posted by scias23 ...
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>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<search.h>
//int joke(const void* x,const void* y);
void main()
{
   clrscr();
   char a[100],temp;
   cout<<"Enter string: ";
   cin>>a;
   for(int i=(strlen(a)-1);i>=0;i--)
      cout<<a[i];
   cout<<"\n\nLETTERS IN THE STRING: ";
   for(int b=0;b<(strlen(a)-1);b++)
   {
      for(int c=b+1;c<(strlen(a));c++)
      {
	 if(a[b]<a[c])
	 {
	    temp=a[b];
	    a[b]=a[c];
	    a[c]=temp;
	 }
      }
   }
   cout<<a;
   getch();
}
You need to clean up your coding habits -just a bit- to help yourself out, while working with it:
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.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jan 2nd, 2009
0

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])) .
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Jan 2nd, 2009
0

Re: sorting characters in a string... help!!!!

Thanks for all the help! :d
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Segmentation Default Error when Initializing Class object
Next Thread in C++ Forum Timeline: quick synax question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC