[英]In C, sort array of strings by string length
所以我將字符串輸入到數組mydata[10][81]
while ((ct<=10) && gets(mydata[ct]) != NULL && (mydata[ct++][0] != '\0'))
然后我使用 for 循環創建第二個指針數組
for (i=0;i<11;i++){
ptstr[i] = mydata[i];
}
這就是我卡住的地方我知道我需要以某種方式使用strlen
,但我什至無法想象如何獲取指針的長度,然后根據長度的第三個附加值將該指針重新分配一個新位置
希望這是有道理的,我對如何做或解釋它很迷茫,我只是想使用數組位置按長度對字符串進行排序(而不是使用類似qsort
東西)
我做了一些更多的工作,並想出了這個:知道為什么它不起作用嗎?
void orderLength(char *ptstr[], int num){
int temp;
char *tempptr;
int lengthArray[10];
int length = num;
int step, i, j, u;
for (i=0; i<num;i++){
lengthArray[i] = strlen(ptstr[i]);
}
for (step=0; step < length; step++){
for(j = step+1; j < step; j++){
if (lengthArray[j] < lengthArray[step]){
temp = lengthArray[j];
lengthArray[j] = lengthArray[step];
lengthArray[step] =temp;
tempptr=ptstr[j];
ptstr[j]=ptstr[step];
}
}
}
for (u=0; u<num; u++){
printf("%s \n", ptstr[u]);
}
}
正如Deduplicator的評論中所建議的,您可以使用stdlib.h
定義的qsort
。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 4
#define MAXLEN 20
int compare (const void * a, const void * b) {
size_t fa = strlen((const char *)a);
size_t fb = strlen((const char *)b);
return (fa > fb) - (fa < fb);
}
int main(int argc, const char * argv[]) {
char arr[ROWS][MAXLEN] = {
"abcd",
"ab",
"abcdefgh",
"abc"
};
qsort(arr, ROWS, MAXLEN, compare);
return 0;
}
你可以在這里看到它的運行情況。
一個非常簡單的版本可能看起來像這樣。 這是一個冒泡排序,對於任何合理大小的數據來說都很慢,但看起來你只對 11 個元素進行排序,所以在這里無關緊要。
關鍵是比較兩個數組位置的長度的“if”語句。 如果它們亂序,接下來的三行會交換它們。
char* temp;
int length = 11;
int step, i;
for(step = 0; step < length - 1; step++)
for(i = 0; i < length - step - 1; i++)
{
if(strlen(ptstr[i]) > strlen(ptstr[i+1]))
{
temp = ptstr[i];
ptstr[i] = ptstr[i + 1];
ptstr[i + 1] = temp;
}
}
編輯:如果要按字符串的內容而不是長度進行排序,則將 if 語句更改為:
if(strcmp(ptstr[i], ptstr[i + 1]) > 0)
(注意:您最好盡可能使用 str n cmp)
為了避免在同一個字符串上多次調用 strlen(),您可以使用如下所示的結構鏈:
#include <stdio.h>
#include <stdlib.h>
typedef struct t_elem
{
char data[81];
int length;
t_elem *next;
};
int main(int ac, char **av)
{
t_elem *head;
t_elem *recent;
t_elem *current;
while (/* string have to be sorted */)
{
if (head == NULL) {
head = (t_elem *)malloc(sizeof(t_elem));
head->data = //readTheFirstString();
head->length = strlen(head->data);
head->next = NULL;
}
else {
recent = (t_elem *)malloc(sizeof(t_elem));
recent->data = //readTheNextString();
recent->length = strlen(recent->data);
recent->next = NULL;
if (recent->length < head->length) {
recent->next = head;
head = recent;
}
else {
current = head;
while (current->next && current->next->length < recent->length) {
current = current->next;
}
recent->next = current->next;
current->next = recent;
}
}
}
// print the sorted chained list
current = head;
while (current->next) {
printf("%s\n", current->data);
current = current->next;
}
// free the list
current = head;
while (current->next) {
recent = current;
current = current->next;
free(recent);
}
return (0);
}
//C program to sort string based on string length.
#include<stdio.h>
#include<string.h>
int main()
{
char a[200][200],temp[20];
int i,j,n;
printf("Enter no. of strings to be input = ");scanf("%d",&n);
printf("Enter %d strings:\n",n);
for(i=0;i<n;i++)
scanf("%s",&a[i]);
//Sorting string based on length
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strlen(a[j])>strlen(a[j+1]))
{
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp);
}
}
}
printf("After Sorting :\n");
for(i=0;i<n;i++)
printf("%s\n",a[i]);
return 0;
}
//Made By Capricious Coder; Happy Coding :D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.