选择排序字符C
这是我编写的一个程序,应该使用选择排序方法按字母顺序对单词的各个字符进行排序选择排序字符C,c,sorting,char,logic,C,Sorting,Char,Logic,这是我编写的一个程序,应该使用选择排序方法按字母顺序对单词的各个字符进行排序 #include <stdio.h> #include <stdlib.h> #include <string.h> char str[100]; void alphaOrder(){ for(int i=0;str[i]!='\0';++i){ //iterate until end of entered
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char str[100];
void alphaOrder(){
for(int i=0;str[i]!='\0';++i){ //iterate until end of entered string
if(str[i+1]==' ' || str[i+1]==NULL){ // iterate until end of current word
int wordLength=0; // finds amount of letters in current word
for(int j=i;j>=0 && str[j]!=' ';j--){
wordLength+=1;
}
int smallest=1000;
int prv = 1000;
for(int k=0;k != wordLength;++k){
int counter=0; //loops through letters in word printing and storing the smallest
while(counter!=wordLength){
if(k==0){ // this if loop only occurs during for 1st char of word
if(str[i-counter] < smallest){
smallest=str[i-counter];
}
}
else{
if(str[i-counter] > smallest && str[i-counter] < prv){
smallest=str[i-counter];
prv=smallest;
}
}
++counter;
}
printf("%c",smallest);
}
}
}
我知道问题源于逻辑,但已经一周了,我仍然无法正确地实现选择排序算法,或者弄清楚我到底需要什么。
因此,任何建议都是非常受欢迎的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void alphaOrder(char *str){
char *p = str;
while(*p){
while(isspace((unsigned char)*p))
++p;//skip space
if(!*p)//end of string?
break;
char *word = p;//top of word
while(*p && !isspace((unsigned char)*p))
++p;//in word
//selection sort
int word_length = p - word;
for(int i = 0; i < word_length -1; ++i){
int smallest = i;
for(int j = i + 1; j < word_length; ++j){
if(word[smallest] > word[j])
smallest = j;
}
if(i != smallest){
char temp = word[i];
word[i] = word[smallest];
word[smallest] = temp;
}
}
}
}
int main(void){
char str[100+1];
fgets(str, sizeof str, stdin);
alphaOrder(str);
puts(str);
}
#包括
#包括
#包括
#包括
无效字母顺序(字符*str){
char*p=str;
而(*p){
while(isspace((无符号字符)*p))
++p、 //跳过空格
if(!*p)//字符串的结尾?
打破
char*word=p;//单词顶部
while(*p&&!isspace((无符号字符)*p))
++p、 //大写
//选择排序
int-word_-length=p-字;
for(int i=0;i单词[j])
最小=j;
}
如果(i!=最小){
字符温度=字[i];
单词[i]=单词[最小];
字[最小]=温度;
}
}
}
}
内部主(空){
char-str[100+1];
fgets(str,str的大小,stdin);
字母顺序(str);
put(str);
}
您做了哪些调试工作?你印了什么?您在调试器中跟踪什么?处理world
时wordLength
的值是多少?为什么在函数中使用全局变量?您应该将要排序的字符串传递给函数。AFAICS,您首先尝试“排序”,然后是hello
,然后是ello
,然后是llo
,等等,但您没有对字符串进行排序;您正在打印字符,而不是重新排列它们。你不是应该重新整理数据吗?您应该至少将函数拆分为两个-一个用于拆分,一个用于排序。如果单词仅由字母组成,请使用isalpha
而不是isspace
。
input: hello world
output:eoooo ddddd
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void alphaOrder(char *str){
char *p = str;
while(*p){
while(isspace((unsigned char)*p))
++p;//skip space
if(!*p)//end of string?
break;
char *word = p;//top of word
while(*p && !isspace((unsigned char)*p))
++p;//in word
//selection sort
int word_length = p - word;
for(int i = 0; i < word_length -1; ++i){
int smallest = i;
for(int j = i + 1; j < word_length; ++j){
if(word[smallest] > word[j])
smallest = j;
}
if(i != smallest){
char temp = word[i];
word[i] = word[smallest];
word[smallest] = temp;
}
}
}
}
int main(void){
char str[100+1];
fgets(str, sizeof str, stdin);
alphaOrder(str);
puts(str);
}