DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   sorting characters in a string... help!!!! (http://www.daniweb.com/forums/thread165429.html)

MosaicFuneral Jan 1st, 2009 4:43 pm
Re: sorting characters in a string... help!!!!
 
Quote:

Originally Posted by scias23 (Post 768593)
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.

da penguin Jan 2nd, 2009 7:35 am
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]))
.

scias23 Jan 2nd, 2009 8:24 am
Re: sorting characters in a string... help!!!!
 
Thanks for all the help! :d


All times are GMT -4. The time now is 11:37 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC