Post By: Hanan Mannan (H.M.R.A Group Engineers)
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------
Computer Programming - Strings
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------
Computer Programming - Strings
During our discussion about characters in computer programming, we learnt that character data type deals with a single character and you can assign any character from your keyboard to a character type variable.
Now, let's move a little bit ahead and consider a situation where we need to store more than one character in a variable. We have seen that C programming does not allow to store more than one character in a character type variable. So following statements are invalid in C programming and produce syntax error:
char ch1 = 'ab';
char ch2 = '10';
We also have seen how we can store more than one value of similar data type in a variable using arrayconcept. If recap then, here is the syntax to store and print 5 numbers in an array of int type:
#include
main()
{
int number[5] = {10, 20, 30, 40, 50};
int i = 0;
while( i < 5 )
{
printf("number[%d] = %d\n", i, number[i] );
i = i + 1;
}
}
When the above code is compiled and executed, it produces the following result:
number[0] = 10
number[1] = 20
number[2] = 30
number[3] = 40
number[4] = 50
Now, let's define an array of 5 characters in the similar way as we did for numbers and try to print them:
#include
main()
{
char ch[5] = {'H', 'e', 'l', 'l', 'o'};
int i = 0;
while( i < 5 )
{
printf("ch[%d] = %c\n", i, ch[i] );
i = i + 1;
}
}
Here, we used %c to print character value. When the above code is compiled and executed, it produces the following result:
ch[0] = H
ch[1] = e
ch[2] = l
ch[3] = l
ch[4] = o
If you are done with the above example, then I think you understood about strings in C programming, because strings in C are represented as arrays of characters. C programming simplified the assignment and printing of strings. Let's check same example once again with simplified syntax:
#include
main()
{
char ch[5] = "Hello";
int i = 0;
/* Print as a complete string */
printf("String = %s\n", ch);
/* Print character by character */
while( i < 5 )
{
printf("ch[%d] = %c\n", i, ch[i] );
i = i + 1;
}
}
Here, we used %s to print full string value using array name ch, which is actually beginning of the memory address holding ch variable as shown below:
Though it's not visible from the above examples, but internally C program assigns null character '\0' as the last character of every string. This indicates the end of the string and it means if you want to store a 5 character string in an array then you must define array size of 6 as a good practice, though C does not complain about it.
Now if the above code is compiled and executed, it produces the following result:
String = Hell
ch[0] = H
ch[1] = e
ch[2] = l
ch[3] = l
ch[4] = o
Basic String Concepts
Based on the above discussion we can conclude the following important points to remember about strings in C programming language:
-
Strings in C are represented as arrays of characters.
-
We can constitute a string in C programming by assigning character by character into an array of characters.
-
We can constitute a string in C programming by assigning a complete string enclosed in double quote.
-
We can print a string character by character using array subscript or a complete string by using array name without subscript.
-
Though it's not visible from the above examples, but internally C program assigns null character'\0' as the last character of every string. This indicates the end of the string and it means if you want to store a 5-character string in an array then you must define array size of 6 as a good practice, though C does not complain about it.
-
Most of the programming languages provide built-in functions to manipulate strings, i.e., you can concatenate strings, you can search from a string, you can take sub string from the string. For a detail you can check detailed tutorial for C or other programming languages.
Strings in C are represented as arrays of characters.
We can constitute a string in C programming by assigning character by character into an array of characters.
We can constitute a string in C programming by assigning a complete string enclosed in double quote.
We can print a string character by character using array subscript or a complete string by using array name without subscript.
Though it's not visible from the above examples, but internally C program assigns null character'\0' as the last character of every string. This indicates the end of the string and it means if you want to store a 5-character string in an array then you must define array size of 6 as a good practice, though C does not complain about it.
Most of the programming languages provide built-in functions to manipulate strings, i.e., you can concatenate strings, you can search from a string, you can take sub string from the string. For a detail you can check detailed tutorial for C or other programming languages.
0 comments:
Post a Comment