Jump to content
Fórum Script Brasil
  • 0

[C]Ordenar caracteres de uma string em ordem crescente


shiroyamacah

Question

Eu tenho que salvar em uma string o nome que a pessoa mandar e ordenar as letras de forma crescente.
No meu caso eu deixo todas maiúsculas. E pra ordenar eu pensei em converter letra pra número -> ordenar -> transformar número pra letra.

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#define QUANT 30
//STRING -> INT
void CharToInt(char string[], int strint[]){
int i;
for(i=0;i<QUANT;i++){
strint = atoi (string);
}
}
//INT -> STRING
void IntToChar(int strint[], char string[]){
int i;
for(i=0;i<QUANT;i++){
itoa (strint,string,10);
}
}
//Ordena na ordem cresente a string de INT
void Ord(int strint[]){
int i,j,aux;
for(i=0;i<strlen(strint);i++){
for(j=i+1;j<strlen(strint);j++){
if(strint>strint[j]){
aux = strint;
strint = strint[j];
strint[j] = aux;
}
}
}
}
main(){
char string[QUANT];
int strint[QUANT];
printf("Escreva o seu nome:\n");
fgets(string,30,stdin);
strupr(string);
printf("Nome: %s\n",string);
CharToInt(string,strint);
Ord(strint);
IntToChar(strint,string);
printf("Nome: %s\n",string);
return(0);
}

Tem algum outro jeito de fazer Char->Int e Int->Char?
Ou um jeito de ordenar o Char direto?
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152k
    • Total Posts
      651.6k
×
×
  • Create New...