学习日报

1.  题目: 请编写一个C函数,该函数将一个字符串逆序

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
 char *ptr=(char *)malloc(sizeof(char) * 64);
 char *str=(char *)malloc(sizeof(char) * 64);
 char *a;
 int i;
 printf("请输入字符串:\n");
 scanf("%s", ptr);

 a=ptr;
 ptr+=strlen(ptr)-1;
 while(ptr >= a)
 {
  *str++=*ptr--;
 }
 *str='\0';
 printf("%s\n", str-strlen(a));
 /*while(ptr >= a)
 {
  printf("%c", ptr--);
 }*/
 while(1);
 return 0;
}

2.  题目: 请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出

#include <stdio.h>
int change2(int x)       
{
 int s[32] = {0};
 int i,n = 0;
 while(x != 0)
 {
  s[n++] = x % 2;
  x = x / 2;
 }
 printf("二进制数为:");
 for(i = n - 1;i >= 0;i--)
 {
  printf("%d",s[i]);
 }
 printf("\n");
 return 0;
}
int change8(int x)       
{
 int s[32] = {0};
 int i,n = 0;
 while(x != 0)
 {
  s[n++] = x % 8;
  x = x / 8;
 }
 printf("八进制数为:");
 for(i = n - 1;i >= 0;i--)
 {
  printf("%d",s[i]);
 }
 printf("\n");
 return 0;
}
int change16(int x)        
{
 int s[32] = {0};
 int i,n = 0;
 while(x != 0)
 {
  s[n++] = x % 16;
  x = x / 16;
 }
 printf("十六进制数为:");
 for(i = n - 1;i >= 0;i--)
 {
  printf("%d",s[i]);
 }
 printf("\n");
 return 0;
}
int main()
{
 int num, radix;
 printf("输入一个整数:\n");       
 scanf("%d",&num);
 printf("请输入需要转变的进制(2或8或10或16) :\n");
 scanf("%d",&radix);
 switch (radix)
 {
 case 2:
  change2(num);
  break;
 case 8:
  change8(num);
  break;
 case 10:
  printf("十进制数为:%d", num);
  break;
 case 16:
  change16(num);
  break;
 default:
  printf("error!\n");
 }
 
 while(1);
    return 0;
}

3.  题目: 输入一个字符串,计算字符串中子串出现的次字数

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[20], str2[20], *p1, *p2;
    int count=0;
    printf("请输入一个字符串:\n");
    scanf("%s", str1);
 printf("请输入一个子串:\n");
    scanf("%s", str2);
    p1=str1;
    p2=str2;
    while(*p1 != '\0')
    {
        if(*p1 == *p2)
        {
            while(*p1 == *p2 && *p2 != '\0')
            {
                p1++;
                p2++;
            }
        }
        else
            p1++;
        if(*p2 == '\0')
            count++;
        p2=str2;
    }
    printf("字串出现次数为:%d", count);
 while(1);
 return 0;
}

4.  题目: 编写一个C函数,”I am from shanghai ”倒置为”shanghai from am I”,即将句子中的单词位置倒置,而不改变单词内部结构.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
 int i=0;
 char str[1000]={0};//用于存放输入语句,包括空格
 char ch, *p=str;//p指向第一个元素
 //每次循环获取一个字符(包括空格),保存在数组中
 while((ch=getchar()) != '\n')
 {
  str[i] = ch;
  i++;
 }
 //把指针p移到数组的结尾,遇到空格替换为\0
 while(*p != '\0')
 {
  if (*p == ' ')
  {
   *p='\0';//替换
  }
  p++;
 }
 //指针p向前移动,打印每个单词
 while(p != str)
 {
  p--;
  if (*p=='\0')
  {
   printf("%s ", p + 1);
  }
 }
 printf("%s  ", p);
 while(1);
 return 0;
}

5.  题目: 输入一个字符串,同时输入帧头和帧尾(可以是多个字符),将该字符串中合法的帧识别出来.

#include <stdio.h>
#include <string.h>
#define SIZE 1024
int my_strncmp(char *s1, char *s2, int len);
char * find_aim_str(char *head, char *tail, char *src);
int main()
{
    char src[SIZE];
    char head[SIZE];
    char tail[SIZE];

    printf("请输入一个字符串:\n");
    scanf("%s",src);

    printf("请输入帧头:\n");
    scanf("%s",head);
   
    printf("请输入帧尾:\n");
    scanf("%s",tail);
   
    char *result = find_aim_str(head,tail,src);

    printf("结果为: %s\n",result);
 
 while(1);
    return 0;
}
int my_strncmp(char *s1, char *s2, int len)
{
    int i;

    for(i = 0; i < len; i++)
    {
        if(*(s1 + i) != *(s2 + i))
{
            return -1;
}
    }

    return 0;
}
char * find_aim_str(char *head, char *tail, char *src)
{
    char *temp;
   
    int h_len = strlen(head);
    int t_len = strlen(tail);
   
    while(*src != '\0')
    {
        if(my_strncmp(src,head,h_len) == 0)
{
            temp = src;
   src = src + h_len;

   while(*src != '\0')
   {
      if(my_strncmp(src,tail,t_len) == 0)
      {
                  *(src + t_len) = '\0';
 return temp;
      }

      src++;
   }

   return NULL;
}

src++;
    }
    return NULL;
}


猜你喜欢

转载自blog.csdn.net/qq_41036424/article/details/81052828