1. 实现  mystrcpy(), mystrcmp(), mystrcat(), mystrlen() ;

    #include<stdio.h>
    #include<stdlib.h> int mystrlen(char *c)
    {
    int i=;
    while (c[i]!='\0')i++;
    return i;
    }
    int mystrcmp(char *c1,char *c2)
    {
    int i = , j = ;
    for (; c1[i] != '\0'&&c2[i] != '\0'; i++)
    {
    if (c1[i] < c2[i])return -;
    else if (c1[i] == c2[i])continue;
    else return ;
    }
    return ;
    }
    char* mystrcopy(char *c1, char *c2)
    {
    int i = , j = ; while (c2[j] != '\0')c1[i++] = c2[j++]; c1[i] = '\0';
    return c1;
    }
    char* mystrcat(char *c1, char *c2)
    { int i = , j = ;
    while (c1[i] != '\0')i++; while(c2[j] != '\0')c1[i++] = c2[j++]; c1[i] = '\0';
    return c1;
    }
    int main()
    {
    char c1[] = "hello ", c2[] = "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 ;
    }

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

    Input:____hello_________world_ how___are___you___\n

      Output:   5

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

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

Input:____hello_________world_ how___are___you___\n

Output:   hello

world

how

are

You

#include<stdio.h>
#include<stdlib.h> void fun(char *c)
{
int i = ,cnt=;
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[] = "hello world how are you ";
puts(c1);
fun(c1); system("pause");
return ;
}

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;
}

算法实现c语言--03的更多相关文章

  1. 【最全】经典排序算法(C语言)

    算法复杂度比较: 算法分类 一.直接插入排序 一个插入排序是另一种简单排序,它的思路是:每次从未排好的序列中选出第一个元素插入到已排好的序列中. 它的算法步骤可以大致归纳如下: 从未排好的序列中拿出首 ...

  2. 魔方阵算法及C语言实现

    1 魔方阵概念 填充的,每一行.每一列.对角线之和均相等的方阵,阶数n = 3,4,5….魔方阵也称为幻方阵. 例如三阶魔方阵为: 魔方阵有什么的规律呢? 魔方阵分为奇幻方和偶幻方.而偶幻方又分为是4 ...

  3. 一个UUID生成算法的C语言实现 --- WIN32版本 .

    一个UUID生成算法的C语言实现——WIN32版本   cheungmine 2007-9-16   根据定义,UUID(Universally Unique IDentifier,也称GUID)在时 ...

  4. 无限大整数相加算法的C语言源代码

    忙里偷闲,终于完成了无限大整数相加算法的C语言代码,无限大整数相加算法的算法分析在这里. 500位的加法运行1000次,不打印结果的情况下耗时0.036秒,打印结果的情况下耗时16.285秒. 下面是 ...

  5. 数据结构算法集---C++语言实现

    //数据结构算法集---C++语言实现 //各种类都使用模版设计,可以对各种数据类型操作(整形,字符,浮点) /////////////////////////// // // // 堆栈数据结构 s ...

  6. 1164: 零起点学算法71——C语言合法标识符(存在问题)

    1164: 零起点学算法71——C语言合法标识符 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 10 ...

  7. PID算法(c 语言)(转)

    PID算法(c 语言)(来自老外) #include <stdio.h> #include<math.h> //定义PID 的结构体 struct _pid { int pv; ...

  8. 一个UUID生成算法的C语言实现——WIN32版本

    源: 一个UUID生成算法的C语言实现——WIN32版本

  9. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

随机推荐

  1. OpenSceneGraph FAQ 【转】

    1.地球背面的一个点,计算它在屏幕上的坐标,能得到吗? 不是被挡住了吗? 答:计算一个空间点的屏幕坐标,使用osgAPEx::GetScreenPosition函数.当空间点处于相机视空间内(不管它是 ...

  2. Python 实现二维码生成和识别

    今天突然想给自己自己做个头像,然后还是二维码的形式,这样只要扫一扫就可以访问我的主页.然后就开始自己的苦逼之路... 其实实现二维码java,c#,C++等都可以实现:由于自己正在学python,所以 ...

  3. 全文索引-lucene,solr,nutch,hadoop之nutch与hadoop

    全文索引-lucene.solr.nutch,hadoop之lucene 全文索引-lucene.solr,nutch,hadoop之solr 我在去年的时候,就想把lucene,solr.nutch ...

  4. Redis及其Sentinel配置项详细说明

    Redis及其Sentinel配置项详细说明 http://lixiaohui.iteye.com/blog/2315516

  5. Json API接口数据生成

    偶然发现,对前端数据模拟挺好用,没有跨域问题 https://myjson.com/

  6. linux 块设备驱动(五)——块设备应用层的操作

    一: 加载好sbull驱动 root@CarRadio:/# ls /dev/sbull -l brw-r--r-- 1 root root 254, 16 Mar 25 08:25 /dev/sbu ...

  7. ecshop忘记管理员密码

    直接修改数据表 ecs_admin_user, 找到对应的管理员, 同时修改 password 为 2fc3ec4c91d51bee94f4a8ccbdbe5383 和 ec_salt 为1819, ...

  8. Linux 中权限的再讨论( 上 )

    前言 在Linux系统中,用户分为三个部分( 所有者 同组人 其他 ).每个部分的权限又可以赋予读/写/执行权限.这样,文件的权限标记一共包含 9 个权限位.好了,很多朋友对于Linux权限的了解就仅 ...

  9. php判断某字符串是否不以数字或其他特殊字符开头

    if(preg_match("/^[^\d-.,:]/",$addr)){ echo $addr.'不是数字或其他特殊字符开头'; }

  10. Ubuntu 13.10上用户怎样获得root权限,用户怎样获得永久root权限,假设配置root登录

    一.用户怎样获得root权限: 1. 进入terminal 2. 输入sudo  passwd root   并设置password,提示要你输入两次password.自己设定password,一定要 ...