sorting array
This is a discussion on sorting array within the C Programming forums, part of the General Programming Boards category; Hello!
Code:
main() {
char nome[] = "iiooeeuuaa";
int i;
for(i = 0; nome[i] != '
-
sorting array
Hello!
Code:
main() {
char nome[] = "iiooeeuuaa";
int i;
for(i = 0; nome[i] != '\0'; i++) {
int j;
for(j = 0; j < nome[i] != '\0'; j++) {
if(nome[i] < nome[j]){
char aux = nome[i];
nome[i] = nome[j];
nome[j] = aux;
}
}
}
int j;
for(j = 0; nome[j] != '\0'; j++){
printf("%c", nome[j]);
}
printf("\n");
} My program objective is to sort a string.
the output must be: aeiou
but this is what happens: aaeeiioouu
thanks
-
You've reached your objective. The reason it's doubling every letter is because your string has 2 of each letter in it. Are you saying that your objective is also to eliminate duplicates or...?
If you understand what you're doing, you're not learning anything.
-
my objective is to sort the string, but the letters can't be duplicated
eeiiuuooaa is an example
programming -> agimnopr
-
& the hat of GPL slaying
-
Code Goddess
>my objective is to sort the string, but the letters can't be duplicated
The simplest solution is to sort the string, then remove adjacent duplicates:
Code:
#include <stdio.h>
#include <string.h>
void sort(char str[])
{
int i, j;
char save;
if (str[0] == '\0')
return;
for (i = 1; str[i] != '\0'; i++) {
save = str[i];
for (j = i; j >= 1 && save < str[j - 1]; j--)
str[j] = str[j - 1];
str[j] = save;
}
}
void unique(char str[])
{
int i, j;
for (i = 0; str[i] != '\0';) {
if (str[i] == str[i + 1]) {
for (j = i + 1; str[j] != '\0'; j++)
str[j] = str[j + 1];
}
else
++i;
}
}
int main(void)
{
char str[] = "programming";
sort(str);
unique(str);
puts(str);
return 0;
} Notice that I used an insertion sort instead of the bubble sort. It's shorter, easier to understand, and far more efficient in all but the most exceptional circumstances. Here's some homework for you:
- Why does the sort function terminate if the first element is a null character?
- What purpose does the save variable serve in my sort function?
- Why is the increment section of the outer loop in unique empty?
- Do either of these functions access indices outside of the string's boundaries?
- Make sure that this pair of functions actually works for any input. Consider long strings of duplicates, duplicates at the front and back of the string, and a string with nothing but duplicates.
With that out of the way, can you think of any other ways to solve the problem? What I showed you is not the best solution by any stretch of the imagination.
My best code is written with the delete key.
-
ATH0
Hlo al. i ned a prgm 2 do this
- Why does the sort function terminate if the first element is a null character?
- What purpose does the save variable serve in my sort function?
- Why is the increment section of the outer loop in unique empty?
- Do either of these functions access indices outside of the string's boundaries?
- Make sure that this pair of functions actually works for any input. Consider long strings of duplicates, duplicates at the front and back of the string, and a string with nothing but duplicates.
With that out of the way, can you think of any other ways to solve the problem?
m n a hry. plz email me the prgm thx
Quzah.
Hope is the first step on the road to disappointment.
Popular pages Recent additions
Similar Threads
-
By Huskar in forum C Programming
Replies: 3
Last Post: 03-31-2009, 12:34 PM
-
By George2 in forum C Programming
Replies: 16
Last Post: 11-19-2006, 02:17 AM
-
By Mr_LJ in forum C++ Programming
Replies: 3
Last Post: 01-02-2004, 12:01 AM
-
By omalleys in forum C Programming
Replies: 1
Last Post: 07-01-2002, 08:31 AM
-
By goodn in forum C Programming
Replies: 20
Last Post: 10-18-2001, 09:48 AM