【发布时间】:2015-05-21 22:59:42
【问题描述】:
我编写了一个程序来计算用户输入的字符串中字母和单词的出现次数。我现在已经成功地完成了大部分工作,但是,我还必须按字母顺序排列存储在指针数组中的单词。我看到一个函数 void sortstring() 应该只是这个,但它似乎根本不起作用。我该怎么办?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void findLetters(char *ptr);
void findWords(char *point);
void sort_string(char *p);
int main()
{
char textStream[100]; //up to 98 characters and '\n\ and '\0'
printf("enter some text\n");
if (fgets(textStream, sizeof (textStream), stdin)) //input up to 99 characters
{
findLetters(textStream);
findWords(textStream);
sort_string(textStream);
}
else
{
printf("fgets failed\n");
}
return 0;
}
void findLetters(char *ptr) //find occurences of all letters
{
int upLetters[26];
int loLetters[26];
int i;
int index;
for (i = 0; i < 26; i++) // set array to all zero
{
upLetters[i] = 0;
loLetters[i] = 0;
}
i = 0;
while (ptr[i] != '\0') // loop until prt[i] is '\0'
{
if (ptr[i] >= 'A' && ptr[i] <= 'Z') //stores occurrences of uppercase letters
{
index = ptr[i] - 'A';// subtract 'A' to get index 0-25
upLetters[index]++;//add one
}
if (ptr[i] >= 'a' && ptr[i] <= 'z') //stores occurrences of lowercase letters
{
index = ptr[i] - 'a';//subtract 'a' to get index 0-25
loLetters[index]++;//add one
}
i++;//next character in ptr
}
printf("Number of Occurrences of Uppercase letters\n\n");
for (i = 0; i < 26; i++)//loop through 0 to 25
{
if (upLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'A'), upLetters[i]);
// add 'A' to go from an index back to a character
}
}
printf("\n");
printf("Number of Occurrences of Lowercase letters\n\n");
for (i = 0; i < 26; i++)
{
if (loLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'a'), loLetters[i]);
// add 'a' to go back from an index to a character
}
}
printf("\n");
}
void findWords(char *point)
{
int i = 0;
int k = 0;
int count = 0;
int j = 0;
int space = 0;
int c = 0;
int len = strlen(point);
char copy[50][100];
char* delim = "{ } . , ( ) ";
char **word;
char *newpoint;
char *newerpoint;
char *token;
int occur[50]; // will store number of occurances of each word
for (; i < 50; i++) //sets all indexes to 0
{
occur[i] = 0;
}
for (i = 0; i < len; i++) //counts # of spaces between words
{
if ((point[i] == ' ') || (point[i] == ',') || (point[i] == '.'))
{
space++;
}
}
word = malloc(sizeof(char*)*(space+1)); //allocates memory to array according to number of words
newpoint = malloc(strlen(point)+1);
strcpy(newpoint, point);
newerpoint = malloc(strlen(point) + 1);
strcpy(newerpoint, point);
token = strtok(newpoint, delim);
for (k; k <= space && token != NULL; k++)
{
word[k] = malloc(strlen(token) + 1);
strcpy(word[k], token);
token = strtok(NULL, delim);
printf("%s\n", word[k]);
}
for (k = 0; k <= space; k++)
{
free(word[k]);
}
}
void sort_string(char *p)
{
int c, d = 0, length;
char *pointer, *result, ch;
length = strlen(p);
result = (char*)malloc(length + 1);
pointer = p;
for (ch = 'a'; ch <= 'z'; ch++)
{
for (c = 0; c < length; c++)
{
if (pointer == ch)
{
*(result + d) = *pointer;
d++;
}
pointer++;
}
pointer = p;
}
*(result + d) = '\0';
strcpy(p, result);
free(result);
}
编辑版本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void findLetters(char *ptr);
void findWords(char *point);
int compare_str (const void *a, const void *b);
int main (void)
{
char textStream[100] = {0}; //up to 98 characters and '\n\ and '\0'
typedef unsigned int size_t;
size_t len = 0;
printf ("enter some text\n");
if (fgets (textStream, sizeof textStream, stdin)) //input up to 99 characters
{
len = strlen (textStream);
textStream[--len] = 0; // strip newline from end of textStream
findLetters (textStream);
findWords (textStream);
}
else
{
printf("fgets failed\n");
}
return 0;
}
void findLetters(char *ptr) //find occurences of all letters
{
int upLetters[26];
int loLetters[26];
int i;
int index;
for (i = 0; i < 26; i++) // set array to all zero
{
upLetters[i] = 0;
loLetters[i] = 0;
}
i = 0;
while (ptr[i] != '\0') // loop until prt[i] is '\0'
{
if (ptr[i] >= 'A' && ptr[i] <= 'Z') //stores occurrences of uppercase letters
{
index = ptr[i] - 'A';// subtract 'A' to get index 0-25
upLetters[index]++;//add one
}
if (ptr[i] >= 'a' && ptr[i] <= 'z') //stores occurrences of lowercase letters
{
index = ptr[i] - 'a';//subtract 'a' to get index 0-25
loLetters[index]++;//add one
}
i++;//next character in ptr
}
printf("Number of Occurrences of Uppercase letters\n\n");
for (i = 0; i < 26; i++)//loop through 0 to 25
{
if (upLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'A'), upLetters[i]);
// add 'A' to go from an index back to a character
}
}
printf("\n");
printf("Number of Occurrences of Lowercase letters\n\n");
for (i = 0; i < 26; i++)
{
if (loLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'a'), loLetters[i]);
// add 'a' to go back from an index to a character
}
}
printf("\n");
}
void findWords(char *point)
{
int i, k, count, space;
int len = strlen (point);
char *delim = "\n { } . , ( ) ";
char **word = NULL;
char *newpoint = NULL;
char *token = NULL;
i = k = count = space = 0;
for (i = 0; i < len; i++) //counts # of spaces between words
if ((point[i] == ' ') || (point[i] == ',') || (point[i] == '.'))
space++;
word = malloc (sizeof *word * space + 1); //allocates memory to array according to number of words
newpoint = malloc (strlen (point) + 1);
strcpy (newpoint, point);
token = strtok (newpoint, delim);
printf ("\nSeparating and saving words in pointer array:\n\n");
for (k = 0; token != NULL; k++)
{
word[k] = malloc (strlen (token) + 1);
strcpy (word[k], token);
token = strtok (NULL, delim);
printf ("%s\n", word[k]);
}
count = k; /* save number of string in word */
qsort (word, count, sizeof *word, compare_str); /* sort the array of pointers */
printf ("\nSorted words in pointer array:\n\n");
for (k = 0; k < count; k++)
printf ("%s\n", word[k]);
for (k = 0; k < count; k++)
{
free(word[k]);
}
}
int compare_str (const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return strcmp(*ia, *ib);
}
【问题讨论】:
-
你可以使用 qsort() 吗?这可能是无需自己编写即可进行排序的最简单方法。