RSS Forums RSS

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

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 10
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23 scias23 is offline Offline
Newbie Poster

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

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

if i enter:

then the descending order of string must appear:

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??
AddThis Social Bookmark Button
Reply With Quote  
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!!!!

  #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  
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 4
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

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

  #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  
Posts: 10
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23 scias23 is offline Offline
Newbie Poster

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

  #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 8:04 am. Reason: for clarification
Reply With Quote  
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 4
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

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

  #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  
Posts: 10
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23 scias23 is offline Offline
Newbie Poster

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

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

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

  #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  
Posts: 10
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23 scias23 is offline Offline
Newbie Poster

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

  #8  
Jan 1st, 2009
Thanks sa penguin!!!
Reply With Quote  
Posts: 10
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23 scias23 is offline Offline
Newbie Poster

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

  #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  
Posts: 2,896
Reputation: VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of 
Solved Threads: 366
VernonDozier VernonDozier is offline Offline
Posting Maven

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 876 | Replies: 12 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:29 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC