Chapter 13 Strings - Revision (C-Programming Language, a Modern Approach (KING))
#include
{
int ch, i=0;
if (i < n)
str[i++] = ch;
str[i] = '\0';
return i;
}
{
// (1) Split a string over 2 lines
printf("(1) hello \
there\n");
printf("(1) hello "
"there\n");//----------------------------------------------------------------
// (2) A string literal gets stored in a char array of length n + 1 (special character \0)
// with a pointer of type char * pointing at it. (just like how printf's first argument is of type char *)
//*p = "ab"; // crash
// (3) Declaring a string using:
char s[6] = "hello"; // + 1 for \0
// (5) Difference between char t[] and char* t
// b-Can make the pointer version point to ther stuff and not the array
// c-If both needed do char *p and char t[N] and then p = t;
// (6) printf with %.7s prints 7 chars from s!!
printf("(6) %.3s", s);
// (7) printing using puts and reading with scanf and gets
//b- use %ns with scanf to specify the number with chars
//c- scanf and gets are unsafe using your own example read_line defined above
// (8) looping thru a string until string[i]='\0' instead of having to check for length
// (9) C string library: strcpy, strncpy, strlen, strcat, strncat, strcmp
// strcmp:
// if the chars match between s1 and s2 and s1 has fewer chars then s1 < s2
// (10) while (*p++ = *s++) terminates because all chars except for the null char test true
// (11) planets[][8] = {"earth",....} rows is determined from elements and 8 columns vs
// *planets[] = {"earth", ....} more efficient
}
No comments:
Post a Comment