Know about stringsAuthor : Pandu Ranga Rao N
E - Mail : pandu2050@gmail.com

In C, the char data type only stores a character. Then, how one can declare a string variable in C? Fortunately or unfortunately, I feel, that, the inventors of C language did not think about it when they designed the language basics! What I myself think and feel is that, later on they found this mistake of theirs and added the concept of character arrays or character pointers into C.

So, string is a sequence of characters stored in a character type array, which is terminated with a null character which is denoted by \0 or denoted by NULL, which is predefined constant.

As a rule in C, the character array must be declared using array declaration syntax,


char string_name[maximum-string-size];

The maximum number of characters in an array type declaration of string is fixed where as the arbitrary length declaration stretches size of string to the maximum possible length, which is determined by the computer system’s memory configuration.

Declaring a string of arbitrary length is as follows,


char string_name[];
char *string_name;

In C, we can initialize a value to a string variable using the following statements,


char string_name[maximum-string-size] = {‘c1’, ‘c2’, ’c3’,…};
char string_name[] = {‘c1’, ‘c2’, ’c3’,…};
char *string_name= "String-itself";

In above first two cases, c1, c2, c3 are the three characters, which are supposed to store in the string variable string-name. While using the pointer variable, one can directly specify the string itself, rather than assigning values character by character.

In some cases, the string initialization also takes place in this way.

By using the statement " string_name="string-itself"; ", you can assign the string value to the pointer variable in the middle of the program.

Now, let us look into a program, which initializes string and prints the same.


#include<stdio.h>
void main(void)
{
 char str1[6] = {‘H’, ‘E’, ‘L’,’L’,’O’};
 printf("The string is %s", str1);
}

 


In the above mentioned program, we have assigned values H, E, L, L, O character wise. This forms a string called "HELLO" and it is printed on the screen.

Next, we will look into a program which reads a string and display on to the screen character wise and also display’s the total number of characters found in the string.


#include<stdio.h>
void main(void)
{
 char str1[25];
 int i=0;
 printf("Enter a string: ");
 scanf("%s", str1);
 printf("\nThe string which is read from the key board is, ");
 while(str1[i] != NULL)
 {
   printf("%c", str1[i]);
   i = i + 1;
 }
 printf("The characters that are found in the given string is %d.", i);
}

The above program displays the characters which are found in a given string. In the program, the variable i is mainly used for maintain the index of array elements. Initially, value of the variable I will be set to 0 and goes on incrementing, which is stopped when the array element reaches NULL value. The value of index will be used to determine the number of characters, which the string variable has got.

By my own experience, I would like to say that, one can easily develop their own programming logic, if they are well versed with string and array handling independently without using the built-in functions, for reversing, merging methodology.


Author : Pandu Ranga Rao N
E - Mail : pandu2050@gmail.com
 
 
 
 
 
 
  About Team ProgrammingBasics.In

Contact Team ProgrammingBasics.In

Instructions for submitting your own article
 
 
X

About Team ProgrammingBasics.In

Right now, we are a small but, dedicated team who strived day and night to alter the pretty old programmingbasics.in into a new conceptual website "The ProgrammingBasics Foundation Library".

The articles and topics in the website are the major contributions of freelancers, programmers across the globe. We thank each of them for their valuable contributions. You can also become a contributor by sending some software related articles to us. We will publish it in our website. Instructions for the same are given in the "Instructions for submitting your own article" page.

Contact Us

ICan Technologies,
HIG 44, 23rd Cross, 8th Main,
E-Block, Vijayanagar 3rd Stage
Mysore-570017, Karnataka, India

Website: www.icantech.in
Mobile: +918050239039

Instructions for submitting your own article

Want to become an article contributor to this site ?

If yes, then send us an article written by you on your own words, neatly typed in MS Word document. If we feel that, you have written the article by using some other article then we will reject the article. Every article we will cross verify, before we publish them. The right to reject the article is with us.

If you want to publish your name and email id's, specify the same in the document.

NOTE: You must give your consent in the email for publishing your email id and your name. Otherwise, your name and email-ids will not get published. We respect your privacy.

One article per document, which means, you must not send more than one article in one document. Everything in the sent document will be treated as one article and your article will get rejected without looking in, further.

The articles must and should be related to computer basics, programming concepts and programming languages. We will not entertain any other topics. Please bear this in your mind, before starting an article write-up.

Articles published in ProgrammingBasics.in are the sole property of ICan Technologies, Mysore. You are here in authorizing ICan Technologies, Mysore to take the ownership of the published article. Rejected articles will not be published and you can try to publish the same in other sites. We will inform you the status of your submitted article within 10 working days, from the date of sent mail. If not responded within 10 days, then you are free to try publishing your article on some other websites. We will simply discard them and will not publish them any further. Once we publish the article, you will not be allowed to publish as it is, in some other website. If we find it, then your article will be removed from site and the removal will be informed to you.

Send your articles to programmingbasics.info@gmail.com with "Article for Publishing" in the subject line.

You must create your own images in PSD (Photoshop Document) only, which is to be supported in CS3 or lower versions. Any animated stuffs, must be in fla files (Flash editable file) and not in any other format and must be readable in Falsh 8.0.

You are now in