introduction to strings in c
a string is actually called as a character array.to use strings we should link string header file in the beginning of the program. single variable stores single character, so we should use array to store the words are sentences .strings are always ended with null character (\0), because this defines the end of a string array .if there are n characters in a string,the string arrays need size of n+1 because of null character at the end. strings are always enclosed by double quotes and characters are enclosed by single quotes. String are declared exactly same as arrays. Remember the size of character string should be greater at least by 1 as compared to its corresponding string constant being assigned because of null character is automatically declared. below are some examples.
declaration:
the above example is a string array. in this type of declaration, there is no need of declaring null character, c automatically does for you. the size of the above example is 5 including the null character
by the above example we can understand the concept of null pointer, if we declare the array by single characters we should use single quotes and at last, we should mention null character too.
example:
output:
assignment of the string during the program:
scanf() can be used to take values during program execution as same as numeric values. we don’t need to use &prefix to the variable to define the address of the variable, because string variables are arrays. The program with scanf is space between the words or string. once scanf() encounters an space ,it ignores the rest of the string.
syntax:
example:
gets() and puts():
so, to avoid these kind of problems ,we should use gets() and puts() functions.gets() can take a complete string with spaces.gets() is replaced by scanf() function.printf() is replaced by puts(),these both functions do same tasks .but these both gets() and puts() doesn't require any control strings
lets modify above example
example with gets() and puts();
output:
there are some string functions in next lessons