请教2道编程题。

komilu 2003-10-18 06:16:31
1:输入一个整数,输出它是几位数。


2:比较字符串a和字符串b的大小,返回他们第一个不相同的字符的位置,相同是返回0。


(请用C语言编制)
...全文
65 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
WindFroce 2003-10-26
  • 打赏
  • 举报
回复
写错拉,换个函数名吧
WindFroce 2003-10-26
  • 打赏
  • 举报
回复
再来一个


int strcmp(char *str,char *str1)
{
int ipos = 0;
ipos = strcmp(str,str1);
if(ipos<0)
{
ipos = 1;
for(;*str==*str1&&*str!='\0';str++,str1++)
ipos++;
}
if(ipos>0)
{
ipos = 1;
for(;*str==*str1&&*str1!='\0';str++,str1++)
ipos++:
}

return ipos;
}
WindFroce 2003-10-26
  • 打赏
  • 举报
回复
我也来一个吧!

#include <stdio.h>

int main()
{
int count = 0;
char a[100];
char *s;
printf("%s","please input your number");
scanf("s",a);
s = a;
for(;*s!='\0';s++)
count++;
printf("%d",count):
return 0;
}
bug:如果你输入超过一百位,就出错拉!还有就是没有对输入整形数作校验.哈哈!楼住自己想办法吧!
verbal 2003-10-22
  • 打赏
  • 举报
回复
有sprintf函数吗?是不是我知识浅陋啊?
我只知道有printf(),*_*
fengcai0327 2003-10-22
  • 打赏
  • 举报
回复
int My_Cmp(const char *str1,const char *str2)
{
int Len1=strlen(str1);
int Len2=strlen(str2);
int N=(Len1<=Len2)?Len1:Len2;
int i_Loacate=0;
while(*(str1+i_Loacate)==*(str2+i_Loacate)&&i_Loacate<N)
{
i_Loacate++;
}
if(i_Loacate==N&&Len1==Len2)
{
return 0;
}
else
{
return i_Loacate+1;
}

}

int Count_Numbers(int N)
{

int i=1;
int m_Dvider;
do
{
m_Dvider=N/10;
N=m_Dvider;
i++;
}
while(m_Dvider>0);
return i--;
}
fengcai0327 2003-10-22
  • 打赏
  • 举报
回复
想知道sprintf()函数的具体用法可以去看MSDN,上面的介绍很清楚!
TianGuangZao 2003-10-21
  • 打赏
  • 举报
回复
http://www.eskimo.com/~scs/C-faq/q12.23.html
http://www.eskimo.com/~scs/C-faq/q7.1.html
fifo333 2003-10-21
  • 打赏
  • 举报
回复
来自GNU文档(The GNU C Libary):

Deprecated function: char * gets (char *s)

The function gets reads characters from the stream stdin up to the next newline character, and stores them in the string s. The newline character is discarded (note that this differs from the behavior of fgets, which copies the newline character into the string). If gets encounters a read error or end-of-file, it returns a null pointer; otherwise it returns s.

Warning: The gets function is very dangerous because it provides no protection against overflowing the string s. The GNU library includes it for compatibility only. You should always use fgets or getline instead. To remind you of this, the linker (if using GNU ld) will issue a warning whenever you use gets.

也就是说gets函数很危险(DANGEROUS),因为容易溢出且不检查,保留此函数是为了兼容性,最好使用fgets替代(用法我还不太清楚,清高手指点)。

TianGuangZao 2003-10-20
  • 打赏
  • 举报
回复
千万别用 gets()。
gets() 内置的缓冲区很小,有溢出的危险,比如我输入一个字符串,长度大于内置缓冲区,就出现段错误。

该函数现在已经被遗弃了,虽然 k&r 2rd 还是提到了它,是由于书出的早,还没被发现,后来网上很多服务器软件都有这个漏洞,被乘虚而入,造成损失不小。

现在比较推荐用 fgets 和 sscanf 组合来代替它。

如果喜欢研究字符串函数,不妨去看看源码 ,linux 下很容易得到。一般基本库函数不会再用其它库函数来帮助实现,那样看起来会很没有效率,如你两次调用 strlen 函数,花销不小。
fifo333 2003-10-19
  • 打赏
  • 举报
回复
其中malloc函数中参数取10是因为int型最大值位数是10,如果考虑移植性的话可以先判断INT_MAX的位数,这样的话就要相应修改程序。
lxy2651 2003-10-19
  • 打赏
  • 举报
回复
1.
int length(int k)
{
int i = 1;

while( (k/10) != 0 )
{
++i;
k /= 10;
}

return i;
}



hlxyang 2003-10-19
  • 打赏
  • 举报
回复
因为你是新手,所以在这,我就用主函数直接给你实现,算法不是很好,但希望能给你帮助。
#include "stdio.h"
#include "string.h"
main()
{ char str1[20],str2[20]; /*长度可以变化的用define*/
int i;
gets(str1);
gets(str2);
for(i=0;str1[i]!='\0'&&str2[i]!='\0';i++) /*字符串以‘\0’结束
if(str1[i]!=str2[i]) s=i+1; /*数组下标从0开始,所以结算位置应是i+1*/
if(strlen(str1)==strlen(str2)) s=0;
else
s=i+1;
printf("s=%d",s);
}
laomai 2003-10-19
  • 打赏
  • 举报
回复
to(天光早)
你那个输出每一位数的的结果是倒序,呵呵
hlxyang 2003-10-19
  • 打赏
  • 举报
回复
laomai,你只考虑了正整数,还有负数,没考虑!所以我同意天光早的意见
TianGuangZao 2003-10-19
  • 打赏
  • 举报
回复
to Wolf0403(完美废人) 兄:

int
my_strcmp (const char *s1, const char *s2)
{
const char *s11 = s1;

int ret;

while ( (ret = (*(unsigned char *) s1 - *(unsigned char *) s2++)) == 0
&& *s1++);

return ret == 0 && *s2 ? 0 : s1 - s11 + 1;
}

稍修改了一下,验收一下。
kv4000 2003-10-19
  • 打赏
  • 举报
回复
好佩服你们!!
panzhaoping 2003-10-19
  • 打赏
  • 举报
回复
大饱眼福
Wolf0403 2003-10-19
  • 打赏
  • 举报
回复
用 C 语言不用库函数,不是闲得嘛。。。
来个经典的。

int Len(unsigned i)
{
int len = 1;
// 不可能有0位的 unsigned 吧?
//所以不要 do-while 也不要哨兵,解决了
while (i /= 10)
{
len ++;
}
return len;
}

天光兄:你的 my_strcmp 在比较 "hell" 和 "hello"的情况无法对付吧。返回值没法表示是否相等。

修改版本:
int diff(const char * str1, const char * str2)
{
int i = 0;
for (;(str1[i] == str2[i]) && (str1[i]); i++);
return str1[i] || str2[i] ? i + 1 : 0;
}
因为如果相同则返回 0,所以我返回的是第一个不同字符在字符串中 1-based 序号。
fifo333 2003-10-18
  • 打赏
  • 举报
回复
问题1`:

/*输入一个整数,输出它是几位数以及各个位的数值*/

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>

int get_digits(int a,int *p)
{
int n=0;

do{
p[n]=(a%10)>0?(a%10):(-a%10);
a/=10;
++n;
}while(a!=0);

return n;

}

int main()
{
int a=891237489;
int digits;
int i;
int *p;
if( ( p=(int *)malloc(10*sizeof(int)) )==NULL ){
printf("allocation fail!");
exit(EXIT_FAILURE);
}

digits=get_digits(a,p);
printf("the digits of %d is: %d\n",a,digits);
for(i=0;i<digits;++i){
printf("[%d] is: %d\n",i,p[i]);
}


free(p);

system("pause");
return 0;

}
DESL 2003-10-18
  • 打赏
  • 举报
回复
#include<stdio.h>
void main()
{
int c_a,i=0;
scanf("%d",&c_a);
if(c_a==0)
i=1;
while(c_a>0)
{
i+=1;
c_a/=10;
}
printf("%d",i);
}
hoho我也忘记还有一个0了补上0应该算一位数吧...
加载更多回复(13)

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧