✔ 最佳答案
r u ok for process to read text from a file? just implement for sorting and counting code. please check
#include <stdio.h>
#include <string.h>
int main( )
{ char str[] = "apple,apple,boy,boy,boy,boy,boy,cat,cat,cat";
char delims[] = ",";
char *result = NULL;
char str1[64][64];
char str2[64];
int i = 0;
int j = 0;
int k = 0;
int cnt = 0;
result = strtok( str, delims );
while( result != NULL )
{ strcpy( str1[i], result );
i++;
result = strtok( NULL, delims );
}
for ( j = 0; j < i; j++ )
for ( k = 0; k < i - 1; k++ )
if ( strcmp( str1[k], str1[k+1] ) >= 0 )
{ strcpy( str2, str1[k+1] );
strcpy( str1[k+1], str1[k] );
strcpy( str1[k], str2 );
}
for ( j = 0; j < i; j++ )
{ if ( j == 0 ) // initial string
strcpy( str2, str1[0] );
if ( strcmp( str2, str1[j] ) == 0 )
cnt ++;
else
{ printf( "%s - %d\n", str2, cnt );
strcpy( str2, str1[j] );
cnt = 1;
}
}
printf( "%s - %d\n", str2, cnt );
return 0;
}
hope can help u