字符串练习题

来源:互联网 发布:淘宝投诉处理流程 编辑:程序博客网 时间:2024/06/05 07:35
1.请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出#include<stdio.h>  #include<string.h>  char ch[16] = {'0','1','2','3','4','5','6','7',                '8','9','a','b','c','d','e','f'};  //函数声明:将整数转换为任意进制的字符串  charchar *int2sys(int num,int n,int move_bit);  int main()  {      int num;      int n;      int move_bit = 0;      printf("Please input the num:\n");      scanf("%d",&num);      printf("Please input the system(2,8,16):\n");      scanf("%d",&n);      if(n == 2)      {          move_bit = 1;      }      else if(n == 8)      {          move_bit = 3;      }      else      {          move_bit = 4;      }      charchar *result = int2sys(num,n,move_bit);      printf("%d的%d进制为%s\n",num,n,result);      return 0;  }  charchar *int2sys(int num,int n,int move_bit)  {      int i = 0;      int temp = 1;      int len = 0;      char temp_ch;      static char result[33] = {0};      while(num != 0)      {          temp = num & (n - 1);          result[i] = ch[temp];          num = num >> move_bit;      i++;      }      len = strlen(result);      for(i = 0; i < len / 2; i++)      {          temp_ch = result[i];      result[i] = result[len - 1 - i];      result[len - 1 - i] = temp_ch;      }      return result;  }  
2.输入一个字符串,计算字符串中子串出现的次数[objc] view plain copy print?在CODE上查看代码片派生到我的代码片#include<stdio.h>  #include<string.h>  #define MAX_SIZE 1024  int count_child_str(char* p_str,charchar *c_str)  {       int len = 0;       int count = 0;       len = strlen(c_str);       while(*p_str != '\0')       {           if(strncmp(p_str,c_str,len) == 0)       {           count = count + 1;       }       p_str++;       }       return count;  }  int main()  {      int count;      char p_str[MAX_SIZE] = {0};      char c_str[MAX_SIZE] = {0};      printf("Please input parent str:");      scanf("%s",p_str);      printf("\nPlease input child str:");      scanf("%s",c_str);      count = count_child_str(p_str,c_str);      printf("count = %d\n",count);      return 0;  }  
0 0
原创粉丝点击