1.将包含字符数字的字符串分开,使得分开后的字符串前一部分是数字后一部分是字母。例

如“h1ell2o3” ->”123hello”

#include<stdio.h>
#include<stdlib.h> void fun(char *c)
{
char p[], *loc;
loc = c;
int i = ;
while (*c != '\0')
{
if (*loc >= && *loc <= )
{
printf("%c", *loc);
}
else if (*loc >= 'a'&&*loc <= 'z' || *loc >= 'A'&&*loc <= 'Z')
{
p[i++] = *loc;
}
else printf("字符串格式不对\n");
loc++;
c++;
}
p[i] = '\0';
i = ;
while (p[i] != '\0')
{
printf("%c", p[i++]);
}
} int main()
{
char c[] = "h1ell2o3";
fun(c);
system("pause");
return ;
}

2.将 字 符 串 中 的 空 格 替 换 成 “%020” , 例 如 “hello world how ”
->”hello%020%020%020world%020%020%020how%020%020%020%020”

#include<stdio.h>
#include<stdlib.h> void fun(char *c)
{
char *loc = (char*)malloc(strlen(c));
strcpy(loc, c);
int i = ;
while (*loc != '\0')
{
if (*loc == ' ')
{
c[i++] = '%';
c[i++] = '';
c[i++] = '';
c[i++] = '';
}
else
{
c[i++] = *loc;
}
loc++;
}
c[i] = '\0';
printf("%s\n", c);
}
int main()
{
char c[] = "hello world how ";
fun(c);
system("pause");
return ;
}

3.删除字符串中指定的字符。例如 “abcdaefaghiagkl“ 删除‘a’,以后: “bcdefghigkl”

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fun(char c[])
{
char *s=(char*)malloc(strlen(c));
strcpy(s,c);
int i = ;
while (*s != '\0')
{
if (*s != 'a') {
c[i++] = *s;
}
s++;
}
c[i] = '\0';
printf("%s", c); }
int main()
{
char c[] = "abcdaefaghiagkl";
fun(c);
system("pause");
return ;
}

4.删除一个数组中重复的元素。例如 1 ,2, 2,2,3,3,3,4,4,5,5,5,6,6,6 -> 1,2,3,4,5,6

#include<stdio.h>
#include<stdlib.h>
#define N 100 int delete_elem(int arr[], int new_arr[])
{
int i = , new_arr_len = ;
int flag[N] = { };//对每个元素做一个初始标记
for (i = ; i < ; i++)
{
if (flag[arr[i]] == )
{
new_arr[new_arr_len++] = arr[i];
flag[arr[i]] = ;//出现过则标记属性设为1
}
}
return new_arr_len;
} void main()
{
int i, arr[N] = {,,,,,,,,,,,,,,};
int new_arr[N];
int arr_len = ;//输入number的个数
int new_arr_len; new_arr_len = delete_elem(arr, new_arr); printf("\n");
printf("new array is :\n");
for (i = ; i < new_arr_len; i++)
{
printf("%d ", new_arr[i]);
}
printf("\n"); system("pause");
}

5.将 字 符 串 中 的 相 邻 的 多 余 空 格 去 掉 , 例 如 (空 格 用 下 划 线 表

示): ”___hello____world___how_are_you__” ->”hello_world_how_are_you”

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fun(char c[])
{
int cnt = ;
char *s = (char*)malloc(strlen(c));
strcpy(s, c);
int i = ;
while (*s != '\0')
{
if (*s != ' ') {
c[i++] = *s;
cnt = ;
}else {
cnt++;
if(cnt==)c[i++]=' ';
}
s++;
}
c[i] = '\0';
printf("%s", c);
} int main()
{
char a[] = { " hello world how are you " }; fun(a);
system("pause");
return ;
}

6.附加题:大整数加法。 实现任意范围的两个整数的加法( 整数的范围用 int 型的变量无法表示,50位)

#include<stdio.h>
#include<string.h>
#define MAX 100
int main()
{ int num1[], num2[];
memset(num1, , sizeof(num1));
memset(num2, , sizeof(num2));//将这些数组清零
char *str1 = "";//用于保存键盘输入的两个待加大整数
char *str2 = "";
int len1, len2, i, j;
len1 = strlen(str1);
len2 = strlen(str2);
j = ;
int max = len1 > len2 ? len1 : len2;
for (i = len1 - ; i >= ; i--)
num1[j++] = str1[i] - ''; /*字符串反转并且转换为数字保存到数组里面*/
j = ;
for (i = len2 - ; i >= ; i--)
num2[j++] = str2[i] - '';
for (i = ; i < max; i++)
{
num2[i] += num1[i];//相加
if (num2[i] >= )
{
num2[i] -= ;
num2[i + ] += ;//产生进位
}
}
if (num2[max]) printf("%d", num2[max]);//如果最高位产生进位
for (i = max - ; i >= ; i--)
printf("%d", num2[i]);
printf("\n"); system("pause");
return ;
}

7.求一个字符串数组的最大值和次大值  void big(char *arr[],int size ,char** big1,char** big2)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void big(char *arr[], int size, char** big1, char** big2)
{
int i = , j = ;
char* max, *min, (*tmp)[] = arr;
*big1 = tmp[];
printf("%s\n", *big1);
for (i = ; i < size; i++)
{
printf("%s ", tmp[i]);
}
printf("\n");
for (j = ; j < size; j++)
{
//printf("\n%s %s\t", arr[i], arr[i + 1]);
if (strcmp(*big1, tmp[j]) < ) {
strcpy(*big2, *big1);
strcpy(*big1, tmp[j]);
}
} printf("\n---------------------------------\n");
printf("the biggest string is %s\n", *big1);
printf("the bigger string is %s\n", *big2);
}
int main()
{
char arr[][] = { "hdua","dade","heoc","das3" ,"heod" };
char *big1 = (char*)malloc(), *big2 = (char*)malloc(), (*a)[] = (char*)malloc();
a = arr; big(arr, , &big1, &big2);
system("pause");
return ;
}

最新文章

  1. iframe子页面点击按钮,执行父页面的点击事件
  2. @MappedSuperclass注解的使用说明
  3. 微信不支持Object.assign
  4. Python正则处理多行日志一例
  5. veridata实验例(3)验证veridata发现insert操作不会导致同步
  6. Javascript日期格式化指定格式的字符串实现
  7. iOS 图片裁剪 + 旋转
  8. [js高手之路]html5 canvas动画教程 - 边界判断与反弹
  9. 利用openssl管理证书及SSL编程第3部分:将MinGW编译的openssl dll导出def和lib供MSVC使用
  10. php 进行跨域操作
  11. python 爬虫之beautifulsoup(bs4)环境准备
  12. ios打包unity应用以及配置签名
  13. TCP 三次握手与四次断开
  14. Spring boot 配置 mybatis xml和动态SQL 分页配置
  15. SpringMVC 网站
  16. JavaScript快速入门-ECMAScript本地对象(Number)
  17. VMware激活码,Acrobat激活码
  18. Win7不能用鼠标双击运行jar文件怎么办?
  19. 10013: 以一种访问权限不允许的方式做了一个访问套接字的尝试【WCF异常】
  20. 第148天:js+rem动态计算font-size的大小,适配各种手机设备

热门文章

  1. OTN 交换&amp;amp; P-OTN有效减少100G 网络成本 (三)
  2. Jstl indexOf 参考
  3. JobClient
  4. python(20)- 列表生成式和生成器表达式练习Ⅱ
  5. 后端程序员看前端想死(二)进入页面之后js分析
  6. 说说设计模式~单件模式(Singleton)
  7. xcode升级到6.0以后遇到的警告错误 原帖链接http://www.cocoachina.com/bbs/simple/?t112432.html
  8. wcf系列(一)--- 寄宿方式
  9. 九度OJ 1118:数制转换 (进制转换)
  10. What the 80/20 Rule Tells Us about Reducing HTTP Requests