c语言编程题,怎么做,求教

编写程序,在主程序中读入一个字符串,再调用一个自编的函数(使用指针作为形式参数),在函数内部通过指针将字符串的内容进行倒置

第1个回答  2017-12-28

挺简单的基础C语言题目,思路大概如下

char *myfunc(char *p, int size)
{
    char *tmp_p1 = p;
    char *new_p, *tmp_p2;
    new_p = malloc(size*sizeof(char));
    tmp_p2 = new_p;
    while(*(tmp_p1++)!='\0')
    {    
        // 把tmp_p1指针移到字符串的末尾
    }
    while(tmp_p1-- != p)
    {
        *(tmp_p2++) = *(tmp_p1);
    }
    return new_p;
}

大概思路就是让一个临时指针跑到p字符串的末尾,然后定义新的指针不断自增,而p的临时指针不断自减,把值赋给新的指针。

代码没有执行过,关于++和--可能会有一位的误差,需要自己调试一下。

第2个回答  2017-12-28
#include<stdio.h>
#include<string.h>
void daozi(char *s)
{
int i,j,c,temp;
c=strlen(s)-1;
for(i=0,j=c;i<j;i++,j--)
    {
        temp=*(s+j); 
        *(s+j)=*(s+i);
        *(s+i)=temp;
    }
}
int main()
{
    char a[20];
    scanf("%s",&a);
    daozi(a);
    printf("%s",a);
   return 0; 
}

本回答被提问者采纳
第3个回答  2017-12-28
#include "stdio.h"

void reverse(char* a,int len){
int i,j,temp;
for (i=0,j=len-1;i<j;i++,j--){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

main()
{
char a[100];
scanf("%s",a);
reverse(a,strlen(a));
printf("%s",a);
}

第4个回答  2017-12-28
简单,但是我不帮你写
第5个回答  2017-12-28
我也不会的啊
相似回答