943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3065
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 1st, 2009
0

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

Expand Post »
help.. i dont know how to sort strings.. for example..

if i enter:
C++ Syntax (Toggle Plain Text)
  1. JOKER

then the descending order of string must appear:
C++ Syntax (Toggle Plain Text)
  1. ROKJE

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

C++ Syntax (Toggle Plain Text)
  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??
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Jan 1st, 2009
0

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!
Reputation Points: 7
Solved Threads: 1
Light Poster
Jenniferlinn is offline Offline
25 posts
since Dec 2008
Jan 1st, 2009
0

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
CPP Syntax (Toggle Plain Text)
  1. string str;
  2. str = "whatever"
and it's easier.
Now this
CPP Syntax (Toggle Plain Text)
  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?
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Jan 1st, 2009
0

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

Click to Expand / Collapse  Quote originally posted by da penguin ...
Look, you have that #include<string.h> right? So why don't you use c++ strings? It's like
CPP Syntax (Toggle Plain Text)
  1. string str;
  2. str = "whatever"
and it's easier.
Now this
CPP Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Jan 1st, 2009
0

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

Introducing bubble sort

CPP Syntax (Toggle Plain Text)
  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.
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Jan 1st, 2009
0

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

THANK YOU VERY MUCH!!! IT WORKS!! but can you please explain to me how this code works.?
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Jan 1st, 2009
0

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

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

Thanks sa penguin!!!
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Jan 1st, 2009
0

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();
}
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
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..

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
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

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