按ASC码对字符串排序
2015-12-27 00:05
549 查看
题目:从键盘接收一行字符串,然后按照字符顺序(ASC码)从小到大进行排序后删除重复的字符。要求使用函数 int rankChar(char str[]) { } 完成,函数返回值为删除字符的个数,打印输入的字符串,删除的字符个数及最后得到的字符串。
#include<stdio.h>
#include<string.h>
#include <conio.h>
int rankChar(char input[], char *output)
{
int i, j, k = 0;
int flag;
int length = strlen(input);
for (i = 0; i < length; i++)
{
for (j = 0; j <length-i-1; j++)
{
if (input[j] >= input[j + 1])
{
char temp;
temp = input[j];
input[j] = input[j + 1];
input[j + 1] = temp;
}
}
}
puts(input);
for (i = 0; i<length; i++)
{
flag = 1;
for (j = 0; j < i; j++)
{
if (output[j] == input[i])
flag = 0;
}
if (flag)
output[k++] = input[i];
}
printf("删除了%d个字符后字符串:",length-k);
output[k] = '\0';
puts(output);
return k;
}
void main()
{
char str_input[50];
char str_output[50] = { "0" };
printf("输入字符串为:");
scanf_s("%s", str_input, 20);
rankChar(str_input, str_output);
getchar();
getchar();
}
#include<stdio.h>
#include<string.h>
#include <conio.h>
int rankChar(char input[], char *output)
{
int i, j, k = 0;
int flag;
int length = strlen(input);
for (i = 0; i < length; i++)
{
for (j = 0; j <length-i-1; j++)
{
if (input[j] >= input[j + 1])
{
char temp;
temp = input[j];
input[j] = input[j + 1];
input[j + 1] = temp;
}
}
}
puts(input);
for (i = 0; i<length; i++)
{
flag = 1;
for (j = 0; j < i; j++)
{
if (output[j] == input[i])
flag = 0;
}
if (flag)
output[k++] = input[i];
}
printf("删除了%d个字符后字符串:",length-k);
output[k] = '\0';
puts(output);
return k;
}
void main()
{
char str_input[50];
char str_output[50] = { "0" };
printf("输入字符串为:");
scanf_s("%s", str_input, 20);
rankChar(str_input, str_output);
getchar();
getchar();
}
相关文章推荐
- Codeforces 612D Educational Codeforces Round 4 D. The Union of k-Segments 离散化+map
- 自动化管理工具Saltstack之匹配篇(5)
- Camera
- 1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise
- ListView的显示和缓存机制
- 学习libvirt-记录
- AsyncTask-异步任务
- linux虚拟机问题备忘
- 蓝桥杯之特殊回文数
- oracle分页显示方法 rownum的使用
- Mac常用工具
- Redux系列01:从一个简单例子了解action、store、reducer
- android旋转动画的两种实现方式
- 实例代码详解正则表达式匹配换行
- 详解SQL死锁检测的方法
- Javascript removeChild()删除节点及删除子节点的方法
- SublimeText自带格式化代码功能之reindent
- AngularJS使用ngMessages进行表单验证
- AngularJS使用angular-formly进行表单验证
- 升级和卸载Oracle数据库软件的命令整理