| | |
sorting characters in a string... help!!!!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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..
how could i implement the strcpy or the strcmp in my code??
if i enter:
C++ Syntax (Toggle Plain Text)
JOKER
then the descending order of string must appear:
C++ Syntax (Toggle Plain Text)
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)
#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??
•
•
Join Date: Dec 2008
Posts: 25
Reputation:
Solved Threads: 1
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!
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!
Look, you have that
and it's easier.
Now this
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?
#include<string.h> right? So why don't you use c++ strings? It's like CPP Syntax (Toggle Plain Text)
string str; str = "whatever"
Now this
CPP Syntax (Toggle Plain Text)
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?
No, ma'am, we are musicians.
•
•
•
•
Look, you have that#include<string.h>right? So why don't you use c++ strings? It's like
and it's easier.CPP Syntax (Toggle Plain Text)
string str; str = "whatever"
Now this
CPP Syntax (Toggle Plain Text)
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?
Last edited by scias23; Jan 1st, 2009 at 9:04 am. Reason: for clarification
Introducing bubble sort 
This works.

CPP Syntax (Toggle Plain Text)
#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; }
No, ma'am, we are musicians.
Well, i just transformed your own code. Used
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
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.
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();
}•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
•
•
•
•
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])
< 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
![]() |
Similar Threads
- Sorting Strings (Java)
- Bubble sorting (Java)
- Hi, everyone I need Help in MIPS random string (Assembly)
- Open In New Window Php (PHP)
- Vectors, Functions, and Sorting... oh my! (C++)
- sorting an array of string (C)
- sorting 2d arrays (C)
Other Threads in the C++ Forum
- Previous Thread: Segmentation Default Error when Initializing Class object
- Next Thread: quick synax question
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






