一波C语言字符数组实用技巧集锦

所属分类: 软件编程 / C 语言 阅读数: 42
收藏 0 赞 0 分享

字符数组倒序

#include <stdio.h> 

 void daoxu(char str[]){ 
   int i; 
   char temp; 
   for(i = 0; i < strlen(str) / 2 ; i ++){ 
     temp = str[i]; 
     str[i] = str[strlen(str) - i-1]; 
     str[strlen(str) - i-1] = temp; 
   } 
 } 

单词计数   

 int wordCount(char str[]){ 
   int i; 
   int count = 0; 
   int word = 0; 
   for (i = 0 ; str[i] != '\0' ; i ++) 
   { 
     if (str[i] == ' ') 
     { 
       word = 0; 
     }else if (word == 0) 
     { 
       word = 1; 
       count ++; 
     } 
   } 
   return count; 
 } 

字符大写转小写

 void mylwr(char str[]){ 
   int i; 
   for (i = 0 ; str[i] != '\0' ; i ++) 
   { 
     if (str[i] >= 'A' && str[i] <= 'Z') 
     { 
       str[i] += 'a' - 'A'; 
     } 
   } 
 } 

字符小写转大写

 void myupr(char str[]){ 
   int i; 
   for (i = 0 ; str[i] != '\0' ; i ++) 
   { 
     if (str[i] >= 'a' && str[i] <= 'z') 
     { 
       str[i] -= 'a' - 'A'; 
     } 
   } 
 } 

字符数组计算字符串长度   

 int mylen(char str[]){ 
   int len; 
   for (len = 0 ; str[len] != '\0' ; len ++); 
   return len; 
 } 

字符串连接 

 void mycat(char str1[],char str2[]){ 
   int i,j; 
   for (i = 0 ; str1[i] != '\0' ;i++); 
   for (j = 0 ; str2[j] != '\0' ; j ++) 
   { 
     str1[i + j] = str2[j]; 
   } 
   str1[i + j] = '\0'; 
 } 

指定长度串接 

void myncat(char str1[],char str2[], int len){ 
   int i,j; 
   for(i = 0; str1[i] != '\0'; i++); 
   for (j = 0; j < len; j++) 
   { 
     str1[i + j] = str2[j]; 
   } 
   str1[i + j] = '\0'; 
 } 

字符数组拷贝     

 void mycpy(char dst[],char src[]){ 
   int i = 0; 
   do  
   { 
     dst[i] = src[i]; 
   } while (src[i++] != '\0'); 
 } 

字符数组指定长度拷贝     

 void myncpy(char dst[],char src[], int len){ 
   int i; 
   for (i = 0; i < len; i++) 
   { 
     dst[i] = src[i]; 
   } 
   dst[i] = '\0'; 
 } 

找出句子中最长的单词 

 void longest(char dst[],char src[]){ 
   int i = 0,j; 
   int count =0; 
   int max = -1; 
   do  
   { 
     if (src[i] ==' ' || src[i] == '\0') 
     { 
       if (count > max) 
       { 
         max = count; 
         for (j = 0; j < count; j++) 
         { 
           dst[j] = src[i - count + j]; 
         } 
         dst[j] = '\0'; 
       } 
       count = 0; 
     }else{ 
       count++; 
     } 
   } while (src[i++] != '\0'); 
 } 

从字符串中提取整形数字

#include <stdio.h> 
 
int getint(char str[], int a[]){//从字符串中提取数字并放在数组中 
  int i = 0; 
  int w = 0; 
  int c = 0; 
  int j, k; 
  do  
  { 
    if (str[i] > '0' && str[i] <= '9') 
    { 
      w++; 
    }else if (w) 
    { 
      j = 0; 
      for (k = w; k > 0; k--) 
      { 
        j *= 10; 
        j += str[i - k] - '0'; 
      } 
      w = 0; 
      a[c] = j; 
      c++; 
    } 
  } while (str[i++] != '\0'); 
  return c; 
} 
 
void main(){ 
  char str[100]; 
  int a[100]; 
  int i, j; 
  gets(str); 
  i = getint(str,a); 
  for (j = 0; j < i; j++) 
  { 
    printf("%d ",a[j]); 
  } 
} 

整形、字符数组型转换

#include <stdio.h> 
#include <stdlib.h> 
 
int sumof1(int x)//求一个数转换成二进制以后1的个数 
{ 
  int countx = 0; 
  while(x) 
  { 
    countx ++; 
    x &= x-1; //每位与一次x - 1;就能消掉最后一个1 
  } 
  return countx; 
} 
 
void main(){ 
 
  char c[10]; 
  int i = 999; 
 
  itoa(i, c, 10);//以10进制转换成字符数组 
  puts(c); 
 
  itoa(i, c, 16);//以16进制转换成字符数组 
  printf("0x%s\n", c); 
 
  itoa(i, c, 8);//以8进制转换成字符数组 
  printf("0%s\n", c); 
 
  itoa(i, c, 2);//以2进制转换成字符数组 
  puts(c); 
 
  i = atoi(c);//再将字符串转成整形 
  printf("%d\n", i); 
 
  printf("%d\n", sumof1(i));//以2进制表示时1的个数 
} 

2016425154607514.jpg (257×176)

更多精彩内容其他人还在看

C++广播通信实例

这篇文章主要介绍了C++实现广播通信的方法,实例讲述了C++ socket广播通信的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C++计算ICMP头的校验和实例

这篇文章主要介绍了C++计算ICMP头的校验和的方法,代码简单实用,对于校验ICMP报文来说有不错的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++设置超时时间的简单实现方法

这篇文章主要介绍了C++设置超时时间的简单实现方法,涉及系统函数setsockopt对套接口的操作,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++实现ping程序实例

这篇文章主要介绍了C++实现ping程序实例,涉及C++对于ICMP数据包的发送与回显处理,具有一定的实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之boost::array的用法

这篇文章主要介绍了C++之boost::array的用法,以实例的形式简单讲述了静态数组的容器boost::array的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C++之Boost::array用法简介

这篇文章主要介绍了C++之Boost::array用法简介,较为详细的分析了Boost::array中的常见用法,并用实例的形式予以总结归纳,需要的朋友可以参考下
收藏 0 赞 0 分享

VC文件目录常见操作实例汇总

这篇文章主要介绍了VC文件目录常见操作实例汇总,总结了VC针对文件目录的各种常用操作,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

VC打印word,excel文本文件的方法

这篇文章主要介绍了VC打印word,excel文本文件的方法,是VC操作文本文件中非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC++获得当前进程运行目录的方法

这篇文章主要介绍了VC++获得当前进程运行目录的方法,可通过系统函数实现该功能,是非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

VC中SendMessage和PostMessage的区别

这篇文章主要介绍了VC中SendMessage和PostMessage的区别,较为全面的分析了SendMessage和PostMessage运行原理及用法上的不同之处,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多