博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法实现c语言--03
阅读量:7119 次
发布时间:2019-06-28

本文共 2819 字,大约阅读时间需要 9 分钟。

  1. 实现  mystrcpy(), mystrcmp(), mystrcat(), mystrlen() ;
    #include
    #include
    int mystrlen(char *c){ int i=0; while (c[i]!='\0')i++; return i;}int mystrcmp(char *c1,char *c2){ int i = 0, j = 0; for (; c1[i] != '\0'&&c2[i] != '\0'; i++) { if (c1[i] < c2[i])return -1; else if (c1[i] == c2[i])continue; else return 1; } return 0;}char* mystrcopy(char *c1, char *c2){ int i = 0, j = 0; while (c2[j] != '\0')c1[i++] = c2[j++]; c1[i] = '\0'; return c1;}char* mystrcat(char *c1, char *c2){ int i = 0, j = 0; while (c1[i] != '\0')i++; while(c2[j] != '\0')c1[i++] = c2[j++]; c1[i] = '\0'; return c1;}int main(){ char c1[100] = "hello ", c2[100] = "woel"; printf("%d\n",mystrlen(c1)); printf("%d\n", mystrcmp(c1, c2)); mystrcat(c1, c2); printf("%s\n", c1); mystrcopy(c1, c2); printf("%s\n", c1); system("pause"); return 0;}

    2.输入一行字符串(单词和若干空格), 输出该行单词个数。

    Input____hello_________world_ how___are___you___\n

      Output:   5

#include
#include
int fun(char *c) { int i = 0,cnt=0; while (c[i] != '\0') { if (c[i] == ' ') { i++; if (c[i] != ' ')cnt++; } else i++; } return cnt;}int main(){ char c1[100] = "hello world how are you "; puts(c1); printf("%d",fun(c1)); system("pause"); return 0;}

3.输入一行字符串(单词和若干空格),输出该行单词(每个单词一行)

Input____hello_________world_ how___are___you___\n

Output:   hello

                world

                how

                are

                       You

#include
#include
void fun(char *c) { int i = 0,cnt=0; while (c[i] != '\0') { if (c[i] == ' ') { i++; continue; } if (c[i] != ' ') { printf("%c", c[i]); i++; if (c[i] == ' ')printf("\n"); } }}int main(){ char c1[100] = "hello world how are you "; puts(c1); fun(c1); system("pause"); return 0;}

4.输入一行字符串,把字符串翻转 

Input: I____am__a__student

Output: student__a__am____I

 

1、直接翻转一下

2、把每个单词找出来,原地自身翻转

#include<stdio.h>

#include<stdlib.h>
#include<string.h>

void fun1(char *c)

{
int i = strlen(c) - 1;
while (i >= 0)
{
printf("%c", c[i]);
i--;
}
//printf("\n");
}
void strcut(char *s, char *sub) //用参数sub来保存结果,所以函数不用返回值了
{
int i=0, j = 0, k = 0;
for (i = strlen(s)-1; i >=0 ; i--)
{
//printf("%c", s[i]);
if (s[i] != ' ')
{
sub[j++] = s[i];
if (s[i-1] == ' '||i==0)
{
sub[j] = '\0';
fun1(sub); printf(" ");
j = 0;
}

}

}
}

int main()
{
char c1[] = "hello world how are you ";
puts(c1);
fun1(c1);
printf("\n");
char ps[100]; //用来保存截取子串的必须是数组,不能是指针
static char s[] = "hello world how are you ";
strcut(s, ps);
//printf("%s\n", ps);
system("pause");
return 0;
}

 

转载于:https://www.cnblogs.com/cthon/p/8886034.html

你可能感兴趣的文章
idou老师教你学Istio 04:Istio性能及扩展性介绍
查看>>
详解CSS的Flex布局
查看>>
域渗透——Pass The Hash & Pass The Key
查看>>
与Flutter第一次亲密接触-Android 视角
查看>>
Java设计模式---单例模式
查看>>
从源码角度看ContentProvider
查看>>
MMP,我说每年年会我怎么老是中不了奖,原来是这样
查看>>
十二、实战底部(二)
查看>>
阿里最全面试116题:阿里天猫、蚂蚁金服、阿里巴巴面试题含答案
查看>>
前端开发必备网站
查看>>
正则表达式大全(汇总)
查看>>
ELK 使用小技巧(第 5 期)
查看>>
Java并发(9)- 从同步容器到并发容器
查看>>
假如时光倒流,我会这么学习Java
查看>>
iOS--React Native浏览器插件
查看>>
一个JSON字符串和文件处理的命令行神器jq,windows和linux都可用
查看>>
Notification Swift 3 0
查看>>
Ionic Cordova实现软键盘的监听 以及操作大全
查看>>
Android小知识10则(下)
查看>>
Flask源码解析:从第一个版本开始阅读Flask源码
查看>>