字符串大小的比较,源代码:word_sort.c:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/*************************初 始 化***************************************/
int i = 0, j = 0, k = 0, m = 0, x = 0, y = 0, num = 1, len = 0;
char str1[1024] = "", *word[20], *swap[1], ch[20] = "";
for (i = 0; i < 20; i++) {
if ((&*word[i] = (char *) malloc(30)) == NULL) {
printf("malloc fail\n");
exit(1);
}
memset(&*word[i], 0, 1024);
}
if ((&*swap[1] = (char *) malloc(30)) == NULL) {
printf("malloc fail\n");
exit(1);
}
memset(&*swap[1], 0, 30);
printf
("\n**************************欢迎光临!**********************************\n\n");
/*************************求单词个数***********************************/
printf("请输入你想要的字符串:");
gets(str1);
printf("您输入的内容是:%s\n", str1);
len = strlen(str1);
printf("您总共输入了 %d 个字符!\n", len);
if (str1[0] == ' ' | | str1[0] == 0) {
printf("没有单词输入!\n");
printf("fuck you!\n");
exit(0);
} else {
for (i = 0; i < len; i++) {
if (str1[i] == ' ' && str1[i + 1] != ' ')
num++;
}
/*************************取出单词*************************************/
printf("你输入的单词是:\n");
while (str1[j] != '\0') {
if (str1[j] != ' ') {
ch[k] = str1[j];
k++;
} else {
strcpy(word[m], ch);
printf("*word[%d] = %s\n", m, word[m]);
k = 0;
m++;
memset(ch, 0, 20);
}
j++;
}
&*word[m] = ch;
printf("*word[%d] = %s\n", m, &*word[m]);
printf("你输入的单词总数是 %d 个!\n", num);
}
/************************比较字符串***********************************/
printf("这些单词从小到大排序是:\n");
for (x = 0; x < m; x++) {
for (y = x + 1; y < m + 1; y++) {
if (strcmp(word[x], word[y]) > 0) {
swap[0] = word[x];
word[x] = word[y];
word[y] = swap[0];
}
}
}
for (x = 0; x < m + 1; x++) {
printf("*word[%d] = %s\n", x, word[x]);
}
printf
("\n**************************谢谢使用!**********************************\n\n");
return 0;
}
编译:gcc -Wall word_sort.c 运行:. /a.out
结果:
**************************欢迎光临!**********************************
请输入你想要的字符串:d c b a f e dd g
您输入的内容是:d c b a f e dd g
您总共输入了 16 个字符!
你输入的单词是:
*word[0] = d
*word[1] = c
*word[2] = b
*word[3] = a
*word[4] = f
*word[5] = e
*word[6] = dd
*word[7] = g
你输入的单词总数是 8 个!
这些单词从小到大排序是:
*word[0] = a
*word[1] = b
*word[2] = c
*word[3] = d
*word[4] = dd
*word[5] = e
*word[6] = f
*word[7] = g
**************************谢谢使用!**********************************
readline()的使用:
当你在需要输入一串有空格的字符串时,scant()函数用不上,而使用gets()函数时又出现下面的警告:
word_sort1.c:(.text+0x1ab): 警告: the `gets' function is dangerous and should not be used.
这样会往往让人很恼火,因为gets()输入时能包含空格,所以有可能让一些有心人通过输入输入一些代码使程序不能够正常的运行,因此会出现这样的警告。现在用readline()会替代这一警告,而且很好用,下面的实例readline.c:
#include <stdio.h>
#include <readline/readline.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
char * p = NULL;
p = (char *)malloc(128);
p = readline("请输入:");
printf("输入的数据是:%s\n", p);
return 0;
}
编译:gcc -Wall readline.c -lreadline 执行:. /a.out
结果:
请输入:My Name Is Xiong Feng!
输入的数据是:My Name Is Xiong Feng!
--
/**************************************/
Name: Xiong Feng
E-mail:linux0818@gmail.com
MSN:linux0818@hotmail.com
QQ:23562033
Address: GuangZhou.China
/**************************************/
没有评论:
发表评论