strcmp、strcpy、strlen的实现
- #include <assert.h>
- char *strcpy(char *dst, const char *src)//使用const来约束src,表明src对应的内容不能被修改。
- {
- assert((dst != NULL) && (src != NULL));//使用断言assert来检验输入参数的有效性
- char *tmp = dst;
- while ((*dst++ = *src++) != '\0')
- /* nothing */;
- return tmp;//返回dst,以便实现链式表达式
- }
- int strlen(const char * str)
- {
- assert( str != NULL );
- const char *cp = str;
- while (*cp++ )
- ;
- return (cp - str - 1 );
- }
- int strcmp(const char *src, const char *dst)
- {
- assert((dst != NULL) && (src != NULL));
- int ret = 0 ;
- while(!(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
- { ++src; ++dst; }
- if ( ret < 0 )
- ret = -1 ;
- else if ( ret > 0 )
- ret = 1 ;
- return ret;
- }
- 1.//整数转换成字符串itoa函数的实现
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- void itoaTest(int num,char str[] )
- {
- int sign = num,i = 0,j = 0;
- char temp[11];
- if(sign<0)//判断是否是一个负数
- {
- num = -num;
- };
- do
- {
- temp[i] = num%10+'0';
- num/=10;
- i++;
- }while(num>0);
- if(sign<0)
- {
- temp[i++] = '-';
- }
- temp[i] = '/0';
- i--;
- while(i>=0)
- {
- str[j] = temp[i];
- j++;
- i--;
- }
- str[j] = '/0';
- }
- 2. //字符串转换成整数atoi函数的实现
- int atoiTest(char s[])
- {
- int i = 0,sum = 0,sign; //输入的数前面可能还有空格或制表符应加判断
- while(' '==s[i]||'/t'==s[i])
- {
- i++;
- }
- sign = ('-'==s[i])?-1:1;
- if('-'==s[i]||'+'==s[i])
- {
- i++;
- }
- while(s[i]!='/0')
- {
- sum = s[i]-'0'+sum*10;
- i++;
- }
- return sign*sum;
- }
- 3.//字符串拷贝函数
- #include "stdafx.h"
- #include <assert.h>
- #include <string.h>
- #include <iostream>
- using namespace std;
- char *srcpy(char *dest,const char *source)
- {
- assert((dest!=NULL)&&(source!=NULL));
- char *address = dest;
- while(*source!='/0')
- {
- *dest++=*source++;
- }
- *dest = '/0';
- return address;
- }
- 4.//判断输入的是否是一个回文字符串
- #include "stdafx.h"
- #include <string.h>
- #include <iostream>
- using namespace std;
- //方法一:借助数组
- bool isPalindrome(char *input)
- {
- char s[100];
- strcpy(s,input);
- int length = strlen(input);
- int begin = 0,end = length-1;
- while(begin<end)
- {
- if(s[begin]==s[end])
- {
- begin++;
- end--;
- }
- else
- {
- break;
- }
- }
- if(begin<end)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- //方法二:使用指针
- bool isPalindrome2(char *input)
- {
- if(input==NULL)
- return false;
- char *begin = input;
- char *end = begin+strlen(input)-1;
- while(begin<end)
- {
- if(*begin++!=*end--)
- return false;
- }
- return true;
- }
- int main(int argc, char* argv[])
- {
- char *s ="1234554321";
- if(isPalindrome(s))
- {
- cout<<"True"<<endl;
- }
- else
- {
- cout<<"Fasle"<<endl;
- }
- if(isPalindrome2(s))
- {
- cout<<"True"<<endl;
- }
- else
- {
- cout<<"Fasle"<<endl;
- }
- cin.get();
- return 0;
- }
- 5.//不使用库函数,编写函数int strcmp(char *source, char *dest),若相等返回0,否则返回-1
- int strcmp(char *source, char *dest)
- {
- assert(source != NULL && dest != NULL);
- while(*source++==*dest++)
- {
- if(*source=='/0'&&*dest=='/0')
- return 0;
- }
- return -1;
- }
- #include <stdio.h>
- void strcat(char *string1, char *string2){
- while(*string1 != '/0')
- string1++;
- while(*string2)
- {
- *string1++ = *string2++;
- }
- *string1++ = '/0';
- }
- int strlen(char *string1){
- int count = 0;
- while(*string1++ != '/0')
- count++;
- return count;
- }
- int main(void)
- {
- char name[100]="wangfeng";
- char *mesg = " is a student!";
- strlen(name);
- puts(name);
- return 0;
- }
- #include <stdlib.h>
- /*
- 这个函数调用的是库函数中的
- strtol()函数,关于这个函数的
- 源代码后面将会给出。
- */
- int my_atoi(char *str)
- {
- return (int) strtol(str, NULL, 10);
- }
- /*
- 下面的两个函数没有调用strtol()函数,
- 而是直接给出了该函数的实现。
- */
- int my_atoi01(const char *str)
- {
- long int v=0;
- int sign = 0;
- while ( *str == ' ') str++;
- if(*str == '-'||*str == '+')
- sign = *str++;
- while (isdigit(*str))
- {
- v = v*10 + *str - '0';
- str++;
- }
- return sign == '-' ? -v:v;
- }
- int my_atoi02(char *str)
- {
- int sign;
- int n;
- unsigned char *p;
- p=str;
- while (isspace(*p) ) p++;
- sign = (*p == '-' ) ? -1 : 1;
- if (*p=='+' || *p=='-' ) p++;
- for (n=0; isdigit(*p); p++)
- n = 10*n + (*p - '0');
- return sign*n;
- }
- int main()
- {
- char * str = "2147483647";
- printf("%d/n",my_atoi(str));
- str = "-2147483648";
- printf("%d/n",my_atoi(str));
- str = "2147483647";
- printf("%d/n",my_atoi01(str));
- str = "-2147483648";
- printf("%d/n",my_atoi01(str));
- str = "2147483647";
- printf("%d/n",my_atoi02(str));
- str = "-2147483648";
- printf("%d/n",my_atoi02(str));
- system("pause");
- return 0;
- }