码迷,mamicode.com
首页 > 编程语言 > 详细

c语言2

时间:2017-03-13 22:22:54      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:linux_c语言

  1. strstr函数的的编写


头文件:#include <string.h>

strstr()函数用来检索子串在字符串中首次出现的位置,其原型为:
    char *strstr( char *str, char * substr );

【参数说明】str为要检索的字符串,substr为要检索的子串。

【返回值】返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL。


#include<stdio.h>
#include<string.h>
int cmp(char* pc,char* pd)
{
        while(*(pc)||*(pd)){
                if(*pc > *pd)
                       return 1;
                if(*pc < *pd)
                       return -1;
                pc++;
                pd++;
        }
        return 0;
 }
 void paixu(char** ps)
 {
         int i,j;
         char* tmp=NULL;
         for(i=0;i<6;i++){
                 for(j=i+1;j<6;j++){
                         if(  cmp( *(ps+i) ,*(ps+j) )==1){
                                 tmp = *(ps+i);
                                 *(ps + i)=*(ps + j );
                                 *(ps + j ) = tmp;
                         }
                 }
         }
 
 }
 int main()
 {
         char* pa[6]={"hello","world","xiyou","shannxi","ljh","nihao"};
         paixu(pa);
         int i;
         for(i=0;i<6;i++)
                 printf("%s\n",pa[i]);
         return 0;
 }

2.

    atoi函数的编写

#include<stdio.h>
#include<string.h>

int atoi1(const char* ps)
{
        int sum;
        while(*ps){
                sum=sum*10+(*ps-‘0‘);
                ps++;
        }
        return sum;
}

int main()
{
        char* ch="123456";
        int sum=atoi1(ch);
        printf("%d\n",sum);
        return 0;
}

3.

 itoa函数的编写

#include<stdio.h>
#include<stdlib.h>
int longth(int a)
{
        int i=0;
        while(a){
                a=a/10;
                i++;
        }
        return i;
}
char* itoa1(int val)
{
        int len;
        char* ps=malloc(sizeof(char)*10);
        len=longth(val);
        len--;
        while(val){
                ps[len]=val%10+48;
                //or (ps+len)=val%10+48;
                val=val/10;
                len--;
        }
        return ps;
}
int main()
{
        int nu=3456;
        char* ps=itoa1(nu);
        printf("%s\n",ps);     
}

4.

字符串的排序

#include<stdio.h>
#include<string.h>
int cmp(char* pc,char* pd)
{
        while(*(pc)||*(pd)){
                if(*pc > *pd)
                        return 1;
                if(*pc < *pd)
                        return -1;
                pc++;
                pd++;
        }
        return 0;
}
void paixu(char** ps)
{
        int i,j;
        char* tmp=NULL;
        for(i=0;i<6;i++){
                for(j=i+1;j<6;j++){
                        if(  cmp( *(ps+i) ,*(ps+j) )==1){
                                tmp = *(ps+i);
                                *(ps + i)=*(ps + j );
                                *(ps + j ) = tmp;
                        }       
                }
        }
        
}
int main()
{
        char* pa[6]={"hello","world","xiyou","shannxi","ljh","nihao"};  
        paixu(pa);
        int i;
        for(i=0;i<6;i++)
                printf("%s\n",pa[i]);
        return 0;
}


    


c语言2

标签:linux_c语言

原文地址:http://12451545.blog.51cto.com/12441545/1906038

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!