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??

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!

Edited 3 Years Ago by Dani: Formatting fixed

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?

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..

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.

THANK YOU VERY MUCH!!! IT WORKS!! but can you please explain to me how this code works.?

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

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();
}

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/clibrary/cctype/toupper.html
http://www.cplusplus.com/reference/clibrary/cctype/tolower.html

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.

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])) .

This question has already been answered. Start a new discussion instead.