what is string in programming
Usually, a string is a sequence of characters. In C language, we can define that, “Strings are array of characters terminated by a null character '\0' ”. Actually, strings are one dimensional array of characters. In such cases, strings are operated by programmers according to the requirement of problems. Any group of characters defined between double quotation marks (“ ”) is a constant string.
A constant string example is given below:
“American University”
If you need to include a double quote in the string, then you must be included a back slash as follows:
“\ “American University\” ”
If you want to print or display a string with double quote then you should write the following line:
printf(“\“Hello ! American University!!\” ”);
The output of this line is:
“Hello ! American University!!”
But if you write the following line:
printf(“Hello ! American University!!”);
Then the output will be:
Hello ! American University!!
Generally, character type strings are frequently used to construct significant and understandable programs. The common functions executed on character strings are as follows:
i. Reading and writing strings
ii. Joining strings together
iii. Copying one string to another string
iv. Comparing strings for equality
v. Pull out or extracting a portion of a string
DECLARING STRING VARIABLES
A string variable is any suitable C variable name and is all the time declared as an array. We have already known about data types in C. In C language string data type is not supported. For this reason, we cannot declare string using string data type. So instead of we use array of type character to create string. The general structure or form of declaration of a string variable is as follows:
char String_Variable_name[size];
Here, char is the data type, which is only used for string declaration, String_Variable_name is the user defined array name and size determines the number of characters in the String_Variable_name.
Strings are array of characters terminated by a null character '\0'.
DECLARATION EXAMPLE
Some examples are as follows:
char country[20];
char city[10];
char uniname[25];
Here, country, city and uniname are the string array variables name respectively. When the compiler allocates a character string to a character array, it automatically supplies a null character (‘\0’) at the end of the string. As a result, the size should be equal to the highest number of characters in the string plus one (1).
When the compiler allocates a character string to a character array, it automatically supplies a null character (‘\0’) at the end of the string.
INITIALIZING STRING VARIABLES
Character arrays are initialized when they are declared. In C programming language a character array
to be initialized in either of the following two structures:
char country[11]= “BANGLADESH”;
char country[11]={‘B’, ‘A’, ‘N’, ‘G’, ‘L’, ‘A’, ‘D’, ‘E’, ‘S’, ‘H’, ‘\0’};
The reason here that, the variable country had to be 11 elements long are that the string BANGLADESH contains 10 characters and one element space is supplied for the null terminator (‘\0’). It is noted that, when we initialize a character array by listing its elements, we must supply explicitly the null terminator (‘\0’). In C programming language, it permits us to initialize a character array without identified the number of elements. In such cases, the size of the array will be decided automatically, based on the number of elements initialized. Consider the following example:
char department[ ]={‘S’, ‘S’, ‘T’, ‘\0’};
This example statement defines the array department as a three (3) element array.
STRING HANDLING FUNCTIONS
Naturally, strings are habitually needed to be operated or manipulated by programmer or user according
to the need of problems. All strings manipulation process can be done manually by the programmer but,
this makes programming complex and large. To solve this problem, the C library sustains a large
number of string handling functions. Sting manipulation library functions are belongs to “string.h”
header file. So when you use string library functions in your program, you must be included “string.h”
header file.
Following are the most commonly used string handling functions:
Function Name Description
strcat () -This is used to concatenate two strings
strcmp( ) -This is used to compares two strings
strcpy() -This is used to copies one string over another string
strlen() -This is used to determine the length of a string
strcat( ) Function
strcat() means string concatenation. This function is used to concatenate (Join) two strings together.
This function takes two arguments, i.e., two strings and resultant string is stored in the first string
specified in the argument.
The general form of strcat( ) function is as follows:
strcat(string1, string2);
Here, string1 and string2 are character arrays which are already described in previous lesson. After executed strcat( ) function , string2 is appended to string1. So the resultant value is stored in string1. In this case removing the null character at the end of string1 and placing string2 from there. C language also permits nesting of strcat() functions which is shown in example-2. Nesting function syntax is as follows:
strcat(strcat(string1,string2),string3);
The nesting function is allowed and concatenates all the three strings together and the resultant string is stored in string1.
strcmp() Function
strcmp( ) means string compare. This function is used to compare two stings, recognized by the function arguments and has two arguments string1 and string2 and the strcmp() function returns an integer value as follows:
♦ if Return value < 0 then it indicates that string1 is less than string2.
♦ if Return value > 0 then it indicates that string1 is greater than string2.
♦ if Return value = 0 then it indicates that string1 is equal to string2.
The general syntax of strcmp( ) function is:
strcmp(string1,string2);
strcpy() Function
strcpy() means string copy. This function is used to copies one string over another string. It works like assignment operator. It has two parameters. One is destination and another is source. Destination may be character type array variable and source is character type array variable or c string constant. The content of source variable or string constant is copied to the destination array variable. The general syntax of strcpy( ) function is as follows:
strcpy(destination array, source arry/source_string);
strlen() Function
strlen( ) means string length. This is used to determine or count and return the number of characters in a string. The return value is received by integer type variable. The general structure of strlen( ) is as follows:
count = strlen(string);
0 মন্তব্য(গুলি):
একটি মন্তব্য পোস্ট করুন
Comment below if you have any questions