您的位置:首页 > 其它

字符串函数实现

2015-09-18 15:24 141 查看
本文介绍了strcpy,strcnpy,atoi,itoa,strcmp,strncmp,字符串逆置的c语言实现(比较简单,或许存在bug)

————————————————————————————————————————————————————————————————————————————

1.strcpy,strncpy:

http://blog.csdn.net/u010944778/article/details/48523021

2.字符串的逆置:

void reverse(char *s)
  {
      char temp;
      char *p=s;//P指向S首
      char *q=s;//q指向S尾
      while(*q)
         { ++q;}
       q--;//这个时候q是指向'/0'的,所以需要往前移动一下
      while(q>p)//交叉移动指针,知道p和q交叉
     {
          temp=*p;
          *p=*q;
          *q=temp;  
          p++;
          q--;
      }
     
 }


void main()      
  {                
      char s[]="asvs";
      printf("before  reverser: %s\n",s);
      reverse(s);  
      printf("after   reverser: %s\n",s);
                   
   }



概括:就是用2个指针加一个临时变量来实现,一个指向尾,一个指向头,然后通过一个中间变量的保存数据,进行数据交换,然后循环,尾指针往前走,首指针往后走,直到位置交叉。

3.itoa:(整数转化为字符串)
void   my_itoa(int number,char *s)
  {
      int num=number,i=0;
      char temp[10];
  
       if(number<0)
       {
           num=-num;
       }
      while(num)
      {
          temp[i]=num%10+'0';
          i++;
          num=num/10;
      }
      if(number<0)
      {
          temp[i++]='-';
          temp[i]=0;
      }
       else
       {
           temp[i]=0;
       }
       i=i-1;
      while(i>=0)
      {
         *s++=temp[i];
          i--;
      }
      *s=0;  }


void main()
  {
      int number=-1212;
      char string[10];
      my_itoa(number,string);
      printf("interger=%d,string=%s\n",number,string);
  
  }



概括:首先判断是正数还是负数,正数先把这个数的每一位求出来然后加上一个'0',它就会隐形的变成一个字符,用一个数组保存,循环,直至每个数都求出来,不过数组里面的数组都是逆置的,然后再将这个数组逆置一下即可。如果是负数,就要先把这个数变成正数先将数据保存下来,最后再加上'-'这个符号,再逆置数组即可。

4.字符串转化为整数:

int  my_atoi(char *s)
  {     
      int sum=0;
        
      if(*s=='-')
      {    ++s; 
        
          while(*s)
        
          {
          
              sum=sum*10+(*s-'0');
          
              s++;
        
          }
        
          return -sum;
      } 
      else
      { 
          while(*s)
          {
              sum=sum*10+(*s-'0');
              s++;
          
          }
         return sum;
      }        
               
 }


void main()
  {
      char s[]="-12345";
      int num;
      num=my_atoi(s);
  
      printf("string=%s,interger=%d\n",s,num);
  }



概括:首先判断有没有‘-’这个符号,没有的话就把数组里面的一个个输剪去'0',就会隐形的变成整型数据,然后通过乘10累加返回sum,如果有'-',就先把指针遍历到下一位,然后按照前面的做法,不过最后返回-sum;

5.字符串比较大小(strcmp,strncmp)

#include <stdio.h>
  #include <assert.h>
  
  int my_strcmp(char *dest,char *source)
  {
      assert((NULL!=dest)&&(NULL!=source));
      while(*dest==*source&&*dest!=0&&*source!=0)
      {
          dest++;
          source++;
  
      }
      return (*dest-*source);
  }
  
  
  void main()
  {
      char a[]="ab";
      char b[]="bbc";
      int ret;
      ret=my_strcmp(a,b);
      printf("%d\n",ret);
  
  }
运行结果:

[lingyun@localhost c_example]$ gcc strcmp.c

[lingyun@localhost c_example]$ ./a.out

-1

#include <stdio.h>
  #include <assert.h>
  
  int  my_strncmp(char *dest,char *source,int n)
  {
  
      assert((dest!=NULL)&&(source!=NULL));
  
          while(n-- && *dest==*source && *dest && *source)
          {
              dest++;
              source++;
          }
          return(*dest-*source);
  }
  
  void main()
  {
      char  a[]="abc";
      char  b[]="asd";
      int ret;
      ret=my_strncmp(a,b,1);
      printf("%d\n",ret);
  }



运行结果:

[lingyun@localhost c_example]$ gcc strncmp.c

[lingyun@localhost c_example]$ ./a.out

0

再贴一下2个排序的方法:

选择排序:

#include <stdio.h>
  
  void main()
  {
      int a[10];
      int i,j,k,tmp;
      printf("input:\n");
      for(i=0;i<10;i++)
      scanf("%d",&a[i]);
      for(i=0;i<9;i++)
      {
          k=i;
          for(j=i+1;j<10;j++)
          {if(a[k]>a[j])
                  k=j;
          }
          if(k!=i)
          {
              tmp=a[i];
              a[i]=a[k];
              a[k]=tmp;
          }
      }
      for(i=0;i<10;i++)
      printf("%4d",a[i]);
      printf("\n");
  }
分组排序:

void sort(int *a, int left, int right)
{
    if(left >= right)/*如果左边索引大于或者等于右边的索引就代表已经整理完成一个组了*/
    {
        return ;
    }
    int i = left;
    int j = right;
    int key = a[left];
     
    while(i < j)                               /*控制在当组内寻找一遍*/
    {
        while(i < j && key <= a[j])
        /*而寻找结束的条件就是,1,找到一个小于或者大于key的数(大于或小于取决于你想升
        序还是降序)2,没有符合条件1的,并且i与j的大小没有反转*/ 
        {
            j--;/*向前寻找*/
        }
         
        a[i] = a[j];
        /*找到一个这样的数后就把它赋给前面的被拿走的i的值(如果第一次循环且key是
        a[left],那么就是给key)*/
         
        while(i < j && key >= a[i])
        /*这是i在当组内向前寻找,同上,不过注意与key的大小关系停止循环和上面相反,
        因为排序思想是把数往两边扔,所以左右两边的数大小与key的关系相反*/
        {
            i++;
        }
         
        a[j] = a[i];
    }
     
    a[i] = key;/*当在当组内找完一遍以后就把中间数key回归*/
    sort(a, left, i - 1);/*最后用同样的方式对分出来的左边的小组进行同上的做法*/
    sort(a, i + 1, right);/*用同样的方式对分出来的右边的小组进行同上的做法*/
                       /*当然最后可能会出现很多分左右,直到每一组的i = j 为止*/
}
int main()
  {
      int a[10],i;
      printf("input the number:\n");
      for(i=0;i<10;i++)
          scanf("%d",&a[i]);
      sort(a,0,9);
      for(i=0;i<10;i++)
          printf("%d ",a[i]);
      printf("\n");
      return 0;
  }


我个人认为快速排序就是利用了一个2分法的思想,先找一个基点把大于它的和小于它的分在两边,然后再利用同样的方法去分别去分左边的部分和右边的部分。

—————————————————————————————————————————————————————————————————————————————

小知识:对于字符数组的赋值;

char str[ ]="i am happy"; 这里不加\0,是因为系统会为你自动加上

char str[ ]={'i',' ','a','m',' ','h','a','p','p','y','\0'};
\0是一定要加的不然strlen的时候大小是随即的,\0和0是一样的

这样定义是最规范的


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: