《算法竞赛入门经典》——重新实现库函数

在学习字符串时,重新实现一些库函数的功能是很有益的。

练习1:只用getchar函数读入一个整数。假设它占据单独的一行,读到行末为止,包括换行符。输入保证读入的整数可以保存在int中。

// 3.4.4-1 只用getchar函数读入一个整数。

#include 
int main(void)
{
    int a[100], i = 0, num = 0;
    while((a[i] = getchar()) && a[i] != '\n')
    {
        num = num*10 + a[i] - '0';
        i++;
    }
    printf("%d\n", num);
    return 0;
}
练习2:只用fgets函数读入一个整数。假设它占据单独的一行,读到行末为止,包括换行符。输入保证读入的整数可以保存在int中。

// 3.4.4-2 只用fgets函数读入一个整数。

#include 
#include 
int main(void)
{
	int i, num = 0;
	char s[100];
	fgets(s, sizeof(s), stdin);
	for(i = 0; i < strlen(s)-1; i++)
		num = num*10 + s[i] - '0';
	printf("%d\n", num);
	return 0;
}
练习3:只用getchar实现fgets的功能,即用每次一个字符的方式读取整行。

// 3.4.4-3 只用getchar实现fgets的功能。

#include 
int main(void)
{
	char s[100];
	int i = 0;
	while((s[i] = getchar()) && s[i] != '\n')
	{
		i++;
	}
	s[i] = '\0';
	printf("%s\n", s);
	return 0;
}
练习4:实现strchr的功能,即在一个字符串中查找一个字符。

// 3.4.4-4 实现strchr的功能,即在一个字符串中查找一个字符。

#include 
char * fun(char * s, char c);
int main(void)
{

	return 0;
}

char * fun(char * s, char c)
{
	while(*s && *s != c)
		s++;
	if(*s == c)
		return s;
	else
		return NULL;
}


你可能感兴趣的:(算法竞赛入门经典)