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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

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

 
0
  #1
Jan 1st, 2009
help.. i dont know how to sort strings.. for example..

if i enter:
  1. JOKER

then the descending order of string must appear:
  1. ROKJE

i think of strcpy() ..but it i dont know what to place in the if().. here's my code..

  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdio.h>
  5. void main()
  6. {
  7. clrscr();
  8. char a[100],temp[100];
  9. cout<<"Enter string: ";
  10. gets(a);
  11. cout<<"\nThe reverse of the string entered is: ";
  12. for(int i=(strlen(a)-1);i>=0;i--)
  13. cout<<a[i];
  14. cout<<"\n\nLETTERS IN THE STRING: ";
  15. for(int b=0;b<=(strlen(a)-1);b++)
  16. {
  17. for(int c=0;c<=(strlen(a)-2);c++)
  18. {
  19. if(strcmp(a[c],a[c+1])<0) //I DON"T THINK THIS IS RIGHT
  20. {
  21. strcpy(temp,a[c]);
  22. strcpy(a[c],a[c+1]);
  23. strcpy(a[c],temp);
  24. }
  25. }
  26. }
  27. getch();
  28. }


how could i implement the strcpy or the strcmp in my code??
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 25
Reputation: Jenniferlinn is an unknown quantity at this point 
Solved Threads: 1
Jenniferlinn Jenniferlinn is offline Offline
Light Poster

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

 
0
  #2
Jan 1st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

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

 
0
  #3
Jan 1st, 2009
Look, you have that #include<string.h> right? So why don't you use c++ strings? It's like
  1. string str;
  2. str = "whatever"
and it's easier.
Now this
  1.  
  2. for(int b=0;b<=(strlen(a)-1);b++)
  3. {
  4. for(int c=0;c<=(strlen(a)-2);c++)
  5. {
  6. if(strcmp(a[c],a[c+1])<0) //I DON"T THINK THIS IS RIGHT
  7. {
  8. strcpy(temp,a[c]);
  9. strcpy(a[c],a[c+1]);
  10. strcpy(a[c],temp);
  11. }
  12. }
  13. }

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?
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

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

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

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..
Last edited by scias23; Jan 1st, 2009 at 9:04 am. Reason: for clarification
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

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

 
0
  #5
Jan 1st, 2009
Introducing bubble sort

  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6. std::string str;
  7. std::cin >> str;
  8.  
  9. char tmp;
  10.  
  11. int i,j;
  12. for(i=0;i<str.size()-1;++i) {
  13. for(j = i+1;j<str.size();++j) {
  14. if(str[i] < str[j])
  15. {
  16. tmp = str[i];
  17. str[i] = str[j];
  18. str[j] = tmp;
  19. }
  20. }
  21. }
  22.  
  23. std::cout << str;
  24.  
  25. return 0;
  26. }
This works.
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

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

 
0
  #6
Jan 1st, 2009
THANK YOU VERY MUCH!!! IT WORKS!! but can you please explain to me how this code works.?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

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

 
0
  #7
Jan 1st, 2009
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
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

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

 
0
  #8
Jan 1st, 2009
Thanks sa penguin!!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

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

 
0
  #9
Jan 1st, 2009
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();
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,809
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #10
Jan 1st, 2009
Originally Posted by scias23 View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC