Hi,
i read a lot of forum in which people want to learn C. Well i am giving this tutorial for all those who want to use strings. All the information provided is written by me and has not been copied from anywhere


Well First of all lets see how to store a string into a string.
First of all you need to initiate a variable which can hold an array of characters. It is necessary to initiate an array of characters rather than just a character:

for example:

char string;

is incorrect because if you store a string rather than a character in it, you cannot access each character of the string indivisually and also when you use such a data type to store different strings in a program they cause errors and also dont save them correctly.

The first thing to do is to initiate the right type of data type for it which is an array of characters. It should be initiated a follows



char string[100];

where char is the data type, string is a variable which can hold an array of characters, and 100 is the size of the array. 100 mean that the variable 'string' can store upto 100 characters.
for example. if we store the string "Jibran Bhat" in it. It would have the following values:

string[0] = J
string[1] = i
string[2] = b
string[3] = r
string[4] = a
string[5] = n
string[6] =
string[7] = B
string[8] = h
string[9] = a
string[10] = t
string[11] = \0


Now lets study what values are stored in the variable. If you notice the first character of the string i.e. 'j' was stored in string[0] rather than string[1]. This is because the count for characters in an array starts from zero. This mean that even though we have initiated the string with string[100], the 100th character would be stored in string[99] and string[100] does not exist. This is an important point that should be kept in mind while programming. Often programmers encounter several bugs in their programs just because they forget that the character count in a string starts from 0.
Now, you may have also noticed that after the last character i.e. 't' stored in string[10], string[11] has the value \0. This \0 is "string terminating character" or Null. This marks the end of the string.

Now that you know the structure of a variable of "array of characters", it would be easy for you to use strings.

Now lets come to storage of strings in a variable.

To store a string in the variable string we can either use an input function such as scanf(),gets() etc or you can put a value yourself. To copy a string to a variable C provides us with a function. This function is strcpy(). its syntax is strcpy(variable, string).
For example we can store the value jibnet in the variable string with the following syntax:

strcpy(string,"jibnet");


This would store value "jibnet" in the variable string. And i hope by now you would know where would each character be stored.

For the time being i have written this much only. I will write more on all the functions and strings in an elobrate manner if you want me to.
If you want me to post more. Then please reply. I would be glad to give you all a nice tutorial on strings or any other topic in C. Although i can teach you C but i dont have any information about C++. I havent learnt C++. But its the same thing.

Anyways, reply to the post if you want me to continue writing