谢霆锋冯德伦关系:[C语言]关于函数的问题希望高手指点一下,非常感谢!

来源:百度文库 编辑:高校问答 时间:2024/05/09 10:07:33
1.编一函数,输入一个四位数,要求分别输出每一位的数字,各数字之间空两格
2.编一函数,把两个字符串连接起来(不能用strcat)

1.
// zd_23.cpp : Defines the entry point for the console application.
//

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

main()
{
char buffer[4];
int i, ch;

printf( "input a num: " );

for( i = 0; (i < 4) && ((ch = getchar()) != EOF)
&& (ch != '\n'); i++ )
buffer[i] = (char)ch;

buffer[i] = '\0';
for(i=0;i<strlen(buffer);i++)
printf("%c\t",buffer[i]);
}

2.
#include<stdio.h>
main()
{
char a[80],b[80],c[160];
int i=j=0;
while(a[i]!='\0')//该循环是把a数组中的元素全部复制到c数组的开始部分
{
c[j]=a[i];
i++; j++;
}
i=0;
while(b[i]!='\0')//该循环是把b数组中的元素全部复制到c数组的接下来的部分
{
c[j]=b[i];
i++; j++;
}
c[j]='\0';//加上字符串的结束标志
puts(c);//输出合并后的字符串
}

回答者:jycvlxp - 试用期 一级 5-20 20:14
什么叫投机取巧?
能用最高的效率达到目的就是好的
我用最简单的方法达到目的也叫投机取巧。。。

/*完美C语言纯正版:)*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
void opr1(int N)
{
int k,h,d,s;
k=(int)(N/1000);
h=(int)((N-k*1000)/100);
d=(int)((N-k*1000-h*100)/10);
s=N%10;
printf("%d %d %d %d\n",k,h,d,s);
}
char* join(char* A,char* B)
{int i,j;
char *temp ;
temp=(char*)malloc(200*sizeof(char));
for(i=0;i<strlen(A);i++)
temp[i]=A[i];
for(i=strlen(A),j=0;i<strlen(A)+strlen(B);i++,j++)
temp[i]=B[j];
temp[strlen(A)+strlen(B)]='\0';
return temp;
}
int main()
{
int n;
char *a;
char s1[100],s2[100];
a=(char*)malloc(200*sizeof(char));
printf("input Number\n");
scanf("%d",&n);
printf("the answer is :\n") ;
opr1(n);/*测试第一个函数*/
printf("input two string:\n");
scanf("%s",s1);
scanf("%s",s2);
a=join(s1,s2);/*测试第二个函数*/
printf("after join the string is:\n%s",a);
getch();
return 0;
}

先说两句
To:Zz小虾米:c语言里面动态申请内存不是用new吧,那可是c++的东西。
To:dadalili: 第一个题目你未免太投机取巧了八。。。。

void fun1(int num4)
{
int a[4];
a[0]=num4%10;/*个位*/
a[1]=(num4%100)/10;/*十位*/
a[2]=(num4/100)%10;/*百位*/
a[3]=num4/1000;/*千位*/
printf("%d %d %d %d",a[3],a[2],a[1],a[0]);
}

char * fun2(char *str1,char *str2)
{
char *temp1,*temp2;
temp1=temp2;
while(*str1)
*(temp2++)=*(str1++);
while(*str2)
*(temp2++)=*(str2++);
*temp2=0;
return temp1;
}
}

dadalili 帅哥救命,我C++遇到了麻烦,又发不了短信息,请进我的帖子看看,救命啊!!!!!!
http://zhidao.baidu.com/question/7518259.html

你给的分好少啊