吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3252|回复: 10
收起左侧

[其他转载] 12.21 小白的C语言

[复制链接]
吾爱姚吕婧妍 发表于 2018-12-21 21:46
本帖最后由 吾爱姚吕婧妍 于 2018-12-22 19:25 编辑

[Asm] 纯文本查看 复制代码
/*1.逆置字符串reverse_str()*/ 
//思路:分开赋值 进行改变 
#include<stdio.h>
#include<string.h>
char reverse_str(char *p)
{
     
  int i,t=0;    //i为循环遍历 t为统计字符有多少个 
  
  /*将p指向最后最后一位置*/ 
  while(*p!='\0')
  {
          p++;
          t++;
  }
  
  char a[t]={0};//等一下做赋值转换使用 
  
  p=p-1;      //因为现在位置为空所以要-1
  
  /*这里逆序赋值给a[]*/ 
  for(i=0;i<t;i++,p--)
  {
          a[i]=*p;         
  }
  
  //这里p位置又为空 所以加+1  原因for循环后最后又循环一次 
 
  p=p+1;
  
  /*在利用逆向好的数组赋值回去给指针 */ 
  for(i=0;i<t;i++,p++)
  {
          *p=a[i];
  }
  
 return 0;        
}

int main()
{
        char *a;
        char b[]="hello";
        a=b; 
    reverse_str(a); 
        printf("%s\n",b);
        return 0;
    
}
[Asm] 纯文本查看 复制代码
/*题目2:将一个字符串,左边N个字节(比如3个字节)旋转到字符串最右边。
比如:"hello world"-->"lo worldhel"。就是将前面3个字符"hel"旋转到了字符串尾部。
*/
//思路:延续题目一 赋值大法 
#include<stdio.h>
#include<string.h>
int rotate_str(char *p,int n)
{
        int o,i,t=0;    //i为循环遍历 t为统计字符有多少个 多了一个o为等下赋值用 
  /*将p指向最后一位 统计其长度*/ 
  while(*p!='\0')
  {
          p++;
          t++;
  }
  char a[t]={0};//等一下做赋值转换 
  
  /*将p指向n+1位置进行赋值*/ 
  for(i=t;i!=n;i--)
  {
          p--;
   } 
 
  //赋值 
  for(i=0;i<(t-n);i++,p++)
  {
          a[i]=*p;         
  }
   o=i;//保存其位置方便下面赋值 
   
  /*将p指向开始 对所需旋转的位置赋值*/ 
   for(i=0;i<t;i++)
   {
           p--;
        } 
        
  //再次赋值 
        for(i=0;i<n;i++,p++,o++)
  {
           a[o]=*p;         
  }
  
  //在赋值回去给指针 
  for(i=0;i<n;i++)
   {
           p--;
        } 
   
   /*利用数组赋值回去*/ 
  for(i=0;i<t;i++,p++)
  {
          *p=a[i];
  }
 return 0;        
}

int main()
{
        int n=3;
        char *a;
        char b[]="hello wll";
        a=b; 
    rotate_str(a,n); 
        printf("%s\n",b);
        return 0;
 } 

免费评分

参与人数 4吾爱币 +6 热心值 +4 收起 理由
iteamo + 2 + 1 每一个帖子我都看了,坚持
wxyzlibo + 1 + 1 我很赞同!
r2003630 + 1 + 1 我很赞同!
苏紫方璇 + 2 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

爱飞的猫 发表于 2018-12-21 23:40

把我的答案也放出来,共同学习

// 1. 逆置字符串 reverse_str()

#include <stdio.h>
#include <string.h>

extern inline
void swap_at(char* p, int i, int j) {
  if (i != j) {
    char tmp = p[i];
    p[i] = p[j];
    p[j] = tmp;
  }
}

void reverse_str(char* str) {
  int last_index = strlen(str) - 1;
  int mid = last_index / 2;
  for(int i = 0; i < mid; i++) {
    swap_at(str, i, last_index - i);
  }
}

int main() {
  char b[] = "hello";
  reverse_str(b);
  printf("%s\n", b);
  return 0;
}
// 题目2:将一个字符串,左边N个字节(比如3个字节)旋转到字符串最右边。
// 比如:"hello world"-->"lo worldhel"。就是将前面3个字符"hel"旋转到了字符串尾部。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define USE_MEMCPY (1)

void rotate_str(char *p, int n) {
  int len = strlen(p);

  // 简单检查
  assert(n <= len);
  assert(n > 0);

#if USE_MEMCPY
  // 方法 1: 利用内存复制移动
  char* tmp = (char *)malloc(n);
  memcpy(tmp, p, n);
  memcpy(p, p + n, len - n);
  memcpy(p + (len - n), tmp, n);
  free(tmp);
#else
  // 方法 2: 手动移动
  for(int i = 0; i < n; i++) {
    char tmp = p[0];

    // 向前移一位
    for (int j = 1; j < len; j++) {
      p[j - 1] = p[j];
    }
    // 补上原本第一位的内容
    p[len - 1] = tmp;
  }
#endif
}

int main() {
  char b[] = "hello world";
  rotate_str(b, 3);
  printf("%s\n", b);
  return 0;
}
wwwmirage 发表于 2018-12-21 21:57
zhuangmm512 发表于 2018-12-21 22:19
yuanjunye 发表于 2018-12-21 22:47
同小白前来学习
mzhsohu 发表于 2018-12-21 23:58
感谢分享!@学习了~!
nj001 发表于 2018-12-22 16:48
逆置字符串
[C] 纯文本查看 复制代码
// Copyright [year] <Copyright Owner>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
void reverse_str(char *a) {
  char b[6];
  for (int i = 0; i < 5; ++i) {
    b[i] = a[4-i];
  }
  b[5] = 0;
  strcpy(a, b);
}
int main() {
    char a[]="hello";
    reverse_str(a);
    printf("%s\n", a);
    return 0;
}


Leo岗 发表于 2018-12-22 17:06
用栈实现会简单一点耗时会小一点
cooper5454 发表于 2018-12-23 12:15
jixun66 发表于 2018-12-21 23:40
[md]把我的答案也放出来,共同学习

```c

你们学的都是什么教程 小学生程度能学吗
 楼主| 吾爱姚吕婧妍 发表于 2019-5-17 18:20
#include<stdio.h>
#include<string.h>
int main()
{
        int check1(char *p);
        int check2(char *q);
        int i;
        char c[12];
        do
        {
        printf("Please input number:");
        gets(c);
        i=check1(c);
        }while(i<0);

        puts(c);//想问问c为什么为空

        return 0;
}

int check1(char *p)
{
        if(strlen(p)<11)
        {
                return -1;
        }
        else
        {

                return check2(p);
        }
}

int check2(char *q)
{

        int i=0;
                puts(q);
        for(;*q=0;q++)
        {
                if(*q<='0'&&*q>='9')
                        i=-2;       
        }
        return i;
}

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-20 01:25

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表