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)

scias23 Jan 1st, 2009 7:44 am
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>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
  clrscr();
  char a[100],temp[100];
  cout<<"Enter string: ";
  gets(a);
  cout<<"\nThe reverse of the string entered is: ";
  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=0;c<=(strlen(a)-2);c++)
      {
        if(strcmp(a[c],a[c+1])<0) //I DON"T THINK THIS IS RIGHT
        {
            strcpy(temp,a[c]);
            strcpy(a[c],a[c+1]);
            strcpy(a[c],temp);
        }
      }
  }
  getch();
}


how could i implement the strcpy or the strcmp in my code??

Jenniferlinn Jan 1st, 2009 8:22 am
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!

da penguin Jan 1st, 2009 8:27 am
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;
str = "whatever"
and it's easier.
Now this
  
for(int b=0;b<=(strlen(a)-1);b++)
  {
      for(int c=0;c<=(strlen(a)-2);c++)
      {
        if(strcmp(a[c],a[c+1])<0) //I DON"T THINK THIS IS RIGHT
        {
            strcpy(temp,a[c]);
            strcpy(a[c],a[c+1]);
            strcpy(a[c],temp);
        }
      }
  }

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?

scias23 Jan 1st, 2009 9:02 am
Re: sorting characters in a string... help!!!!
 
Quote:

Originally Posted by da penguin (Post 768527)
Look, you have that
#include<string.h>
right? So why don't you use c++ strings? It's like
string str;
str = "whatever"
and it's easier.
Now this
  
for(int b=0;b<=(strlen(a)-1);b++)
  {
      for(int c=0;c<=(strlen(a)-2);c++)
      {
        if(strcmp(a[c],a[c+1])<0) //I DON"T THINK THIS IS RIGHT
        {
            strcpy(temp,a[c]);
            strcpy(a[c],a[c+1]);
            strcpy(a[c],temp);
        }
      }
  }

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?

okay, the program should sort the characters in a string in descending order.. for example.. if i cin yahoo, the program should cout yooha.. there..

da penguin Jan 1st, 2009 10:00 am
Re: sorting characters in a string... help!!!!
 
Introducing bubble sort :)

#include <iostream>
#include <string>

int main()
{
        std::string str;
        std::cin >> str;

        char tmp;

        int i,j;
        for(i=0;i<str.size()-1;++i) {
                for(j = i+1;j<str.size();++j) {
                        if(str[i] < str[j])
                        {
                                tmp = str[i];
                                str[i] = str[j];
                                str[j] = tmp;
                        }
                }
        }

        std::cout << str;

        return 0;
}
This works.

scias23 Jan 1st, 2009 10:08 am
Re: sorting characters in a string... help!!!!
 
THANK YOU VERY MUCH!!! IT WORKS!! but can you please explain to me how this code works.?

da penguin Jan 1st, 2009 10:16 am
Re: sorting characters in a string... help!!!!
 
Well, i just transformed your own code. Used
string str
instead of
char* a[100]
and
char tmp
instead 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

scias23 Jan 1st, 2009 10:27 am
Re: sorting characters in a string... help!!!!
 
Thanks sa penguin!!!

scias23 Jan 1st, 2009 10:59 am
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>
#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();
}

VernonDozier Jan 1st, 2009 1:41 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..

if(a[b]<a[c])

If that's the goal, then this line won't work for your purposes. Any lower case letter has a higher ASCII value than an upper case letter. The
<
operator uses the ASCII table. You need to use
toupper
or
tolower
in 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

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 4:16 am.

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