Skip to content

Instantly share code, notes, and snippets.

@arkidaabc
Last active August 29, 2015 13:56
Show Gist options
  • Save arkidaabc/8908114 to your computer and use it in GitHub Desktop.
Save arkidaabc/8908114 to your computer and use it in GitHub Desktop.
5-3-example
//
// main.c
// CH5.3 - Example
// P85 - strlenn: return length of string s
//
#include<stdio.h>
int strlenn(char *m);
int main(void)
{
char test[20] = "hello world"; //correct, this char stores an integer, which equals to the address of the tesst[0]
//printf("please input the test string:");
//scanf("%s", test);
//char test2 = "hello world"; //incorrect, char is a small integer, cannot be assigned by an array char[12]
//correct version of test 2
char *test2;
test2 = "hello world";
printf("the length of string \"hello world\"is :%d\n", strlenn(test));
printf("the length of string - 1 \"hello world\"is :%d\n", strlenn(test+1));
printf("the length of string - 2 \"hello world\"is :%d\n", strlenn(&test[2]));
printf("the length of string \"hello world\"is :%d\n", strlenn(test2));
printf("the length of string \"hello'\\0' world\"is :%d\n", strlenn("hello'\0' world"));
return 0;
}
int strlenn(char *s)
{
int n;
for (n = 0; *s != '\0'; s++) {
n++;
}
return n;
}
@arkidaabc
Copy link
Author

the length of string "hello world"is :11
the length of string - 1 "hello world"is :10
the length of string - 2 "hello world"is :9
the length of string "hello'\0' world"is :6
Program ended with exit code: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment