What is String??
A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers.
int x = 4;
char mystring[] = “prequel”;
Creating a String in C:
- C has no special variable type for strings.
- This standard library provides an extensive range of functions to handle strings.
- Strings in C are stored in an array of type char.
- Characters in a string are stored in adjacent memory cells one character per cell.
- Ex: char myString[20];
- We are just specifying the size but there is no specific data source.
Initializing a String in C:
- You initialize a string variable when you declare it
- char word[] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’ };
- to initialize a string, it is the same as any other array initialization.
- In the absence of an array size, the C compiler automatically computes the number of elements in the array.
- Based upon the number of initializers
- This statement reserves space in memory for exactly seven characters.
- Automatically adds the null terminator.
- You can specify the size of the string explicitly just make sure you leave enough space for the terminating null character.
- char word[7] = {Hello!};
- So do not specify the size let the compiler figure out you can be sure it will be correct.
- You can initialize just part of an array of elements of type char with a string.
- char str[40] = ‘To be’;
- the compiler will initialize the first five elements, str[0] to str[4], with the character of the string constant
- str[5] will contain the null character ‘\0’.
- Space is allocated for all 40 elements of the array.
Example:
#include<stdio.h> int main() { char str[] = "prequelcoding"; printf("%s", str); return 0; }
Displaying a string:
When we wanted to access a string which we store it in an array we simply use the name of the character in the form of input.
- To display a string as output using printf function, you do the following:
printf(“\nThe message is: %s”, message);
- %s is format specifier for outputting a null terminated string.
- The printf function assumes when it encounters the %s format characters that the corresponding argument is a character string that is terminated by the null character.
//output
Inputting a string:
In C programming language we input a string or a statement using the scanf function which will produce us the desired output so let me just give you an example which will crystal clear the previous context.
#include<stdio.h> #include<conio.h> int main() { char str[10]; //storing a string in an array using char datatype printf("hello world"); //we are trying to give the compiler our desired output scanf("%s", str); //we are trying to access the string which we have declared in the beginning of our code using char data type. return 0; }
In the code which we have declared here we are not using the and operand(&) which we use when are going to give the input because %s is itself a string specifier which takes care of the input statements.
Common String Functions:
- We already know that a character string is a char array terminated with a null character (\0)
- Character strings are commonly used.
- C provides many functions specifically designed to work with strings.
- Some of the commonly used strings are:
- Getting length of a string
- strlen
- Copying one character string to another
- strcpy() and strncpy()
- combining two character strings together (concatenation)
- strcat() and strncat()
- determining if two character strings are equal
- strcmp() and strncmp()
Getting Length of the String:
To determine the length of the string we need:
A character string
Strlength
Printf function
String.h library
#include<stdio.h> #include<string.h> /* string header file library which consists of the string functions. */ int main() { char myString[] = "coder space"; printf("The length of string: %ld", strlen(myString)); return 0; NOTE: if you are going to declare %d instead of %ld then you will be getting an error it is because we are here declaring strnlen() which is the sizeof data which will consist of a size_t return type. So be careful while specifying the format ..
Output: