/* atoi: convert s to integer */
int atoi(char s[])
{
nt n = 0, i = 0;
while(‘\0‘ != s[i])
n = n*10 + s[i++] - ‘0‘;
return n;
}
int main(void)
{
char s[] = "12345";
printf("%d\n", atoi(s));
return 0;
}
17.根据描述完成下面的17~20空。
/ *
* DESCRIPTION
* The strchr() function locates the first occurrence of
* c(converted to a char) in the string pointed to s.
* The terminating null character is considered part of the string;
* therefore if c is ‘\0‘, the functions locate the terminating ‘\0‘.
* RETURN VALUES
* The functions strchr() return a pointer to the located
* character, or NULL if the character does not appear in the string.
*/
#include <stdio.h>
char *(strchr) (const char *s, int c)
{
const char ch = (char) c;
const char *sc;
for (sc = 0; ; ++s)
{
if (*s == ch)
sc = s;
if (17)
return ((char *) sc);
}
}
int main(void)
{
char s[] = "Hello world.";
char *p;
p = strchr(s, ‘l‘);
printf("%p %p\n", p, &s[2]);
return 0;
}
答案:‘\0‘==*s || 0!=sc
/ *
* DESCRIPTION
*The strstr() function locates the first occurrence of the
*null-terminated string s2 in the null-terminated string s.
* RETURN VALUES
*if s2 is an empty string, s1 is returned; if s2 occurs nowhere
*in s1, NULL is returned; otherwise a pointer to the first
*character of the first occurrence of s2 is returned.
*/
#include <stdio.h>
#include <string.h>