A string is a sequence of characters stored in order. This order allows us to access characters by their position (index), slice parts of the string, and perform other operations. This organization and ability to manipulate the data make it a data structure, not just plain text.
char str[] = "hello"; printf("%c %s", str[1], &str[2]);
str[1] is 'e' (the second character). &str[2] points to the substring starting at 'l' (third character), so printing it as a string outputs "llo". The output is 'e llo'.
char *str = "hello"; str[0] = 'H';
String literals in C are stored in read-only memory. Trying to modify them causes a segmentation fault at runtime because the program is not allowed to change that memory.
char str1[10] = "Hi "; char str2[] = "there"; int i = 0, j = 0; while (str1[i] != '\0') i++; while (str2[j] != '\0') { str1[i++] = str2[j++]; } str1[i] = '\0'; printf("%s", str1);
The code finds the end of str1, then copies each character from str2 into str1 starting there, and finally adds the null terminator. The result is the concatenated string "Hi there".
Strings keep characters in a fixed sequence. This order allows search algorithms to move through the string and compare sequences efficiently, enabling fast substring or pattern searches.
