C Strings Prabhat Kumar Padhy 1 C Strings
C Strings Prabhat Kumar Padhy 1
C Strings? n Strings ? – Is not this a set of characters ? n ASCII value of ‘ ’ is 0 n Character arrays are often called as strings. n A string can also be initialized as static n String is always terminated by ‘ ’ - NULL char[] = “SRI CITY”; S R I C I T Y 3001 3002 3003 3004 3005 3006 3007 3008 3009 main() { static char name[] = “SRI CITY”; { int i=0; static char name[] = “SRI CITY”; char *ptr; int i=0; ptr = name; //ptr = &name[0]; while (name[i] = ‘ ’) while (ptr = ‘ ’) { { printf(“%c”, name[i]); printf(“%c”, ptr); i++; ptr++; } }
char name[5][100]; C Stings? n String elements can also be accessed by the use of Pointers n printf and scanf possess simple way of handling string I/O Main() { char name[25]; scanf(“%s”, name); // format specification for string is %s printf(“%s”, name); // format specification for string is %s } n String can be one dimensional character array or two dimensional character array char name[5][100]; each line in the 2 D array is an array of char with size = 100 n The above declaration can also be done as below char *name [100]; 3
char name[5][100]; Pointer, Array / Strings? n Pointers and arrays are very closely linked in C. Think of array elements arranged in consecutive memory locations. For Ex: char a[10], *p; P = &a[0]; // pointer “p”, points to address of a[0] S R I C I T Y 3001 3002 3003 3004 3005 3006 3007 3008 3009 n P p++ n There is a subtle difference between pointer and array, such as we can write n n p=a instead p = &a[0] n a[i] can be written as using pointer *(p+i) => &a[i] = (p+i) However pointers and arrays are different n Pointer is a variable (p=a; p++; …. ) n Array is not a variable, rather a Datatype or data structure Important point to understand 4
char name[5][100]; Array of Pointers to Strings? n A pointer, points to string. Set of such pointers pointing to different strings in an array, then it is array of pointers to string 6001 6009 6014 6018 S R I C I T Y I I I T I I T T P T Y void main() { static char names[] = { “SRI CITY”, “IIIT”, “IIT”, IIT TPTY”, Array of pointers names[] 6001 6009 6014 6018 }; Int I; for(i=0; i<3; i++) printf(“%sn”, names[i]); } 5
Basic String Handling Function Syntax Purpose strlen Size_t strlen(const char *str) Finds length of a string strcpy Char * strcpy(char Copy string 2 to string 1. *string 1, const char *string 2) strncpy char *strncpy(char Copy first n characters of string 2 to *string 1, const char stringl *string 2, size_t n) strcasecm p int It can compare the with case strcasecmp(const char *s 1, const char *s 2) strncat char *strncat(char *string 1, char *string 2, size_t n) Strcmp int strncmp(char N no of charactrs 6
Strlen without using builtin function? /* C program to find the length of a string without using the built-in function */ #include <stdio. h> void main() { char string[50]; int i, length = 0; printf("Enter a string n"); gets(string); /* keep going through each character of the string till its end */ for (i = 0; string[i] != '