Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Wednesday 8 January 2014

Strings in C Language

What are Strings
The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array. Character arrays are many a time also called strings. Many languages internally treat strings as character arrays, but somehow conceal this fact from the programmer. Character arrays or strings are used by programming languages to manipulate text such as words and sentences.
A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ). For example,
char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;
Each character in the array occupies one byte of memory and the last character is always ‘\0’.
What character is this? It looks like two characters, but it is actually only one character, with the \ indicating that what follows it is something special. ‘\0’ is called null character. Note that ‘\0’ and ‘0’ are not same. ASCII value of ‘\0’ is 0, whereas ASCII value of ‘0’ is 48.

In C programming, array of character are called strings. A string is terminated by null character /0. For example:
"c string tutorial"
Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string.
Declaration of strings
char s[5];
Strings can also be declared using pointer.
char *p
Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd";
     OR,
char c[5]="abcd";
     OR,
char c[]={'a','b','c','d','\0'};
     OR;
char c[5]={'a','b','c','d','\0'};

String can also be initialized using pointers
char *c="abcd";

The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as follows:
char greeting[] = "Hello";
Following is the memory presentation of above defined string in C/C++:

Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array. Let us try to print above mentioned string:
#include <stdio.h>

int main ()
{
   char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

   printf("Greeting message: %s\n", greeting );

   return 0;
}
When the above code is compiled and executed, it produces result something as follows:
Greeting message: Hello

Reading Strings from user.
Reading words from user.
char c[20];
scanf("%s",c);
String variable c can only take a word. It is beacause when white space is encountered, the scanf() function terminates.
Write a C program to illustrate how to read string from terminal.
#include <stdio.h>
int main(){
    char name[20];
    printf("Enter name: ");
    scanf("%s",name);
    printf("Your name is %s.",name);
    return 0;
}
Output
Enter name: Dennis Ritchie
Your name is Dennis.
Here, program will ignore Ritchie because, scanf() function takes only string before the white space.
While entering the string using scanf( ) we must be cautious about two things:
The length of the string should not exceed the dimension of the character array. This is because the C compiler doesn’t perform bounds checking on character arrays. Hence, if you carelessly exceed the bounds there is always a danger of overwriting something important, and in that event, you would have nobody to blame but yourselves.

scanf( ) is not capable of receiving multi-word strings

Reading a line of text
C program to read line of text manually.
#include <stdio.h>
int main(){
    char name[30],ch;
    int i=0;
    printf("Enter name: ");
    while(ch!='\n')    // terminates if user hit enter
    {
        ch=getchar();
        name[i]=ch;
        i++;
    }
    name[i]='\0';       // inserting null character at end
    printf("Name: %s",name);
    return 0;
}
This process to take string is tedious. There are predefined functions gets() and puts in C language to read and display string respectively.
int main(){
    char name[30];
    printf("Enter name: ");
    gets(name);     //Function to read string from user.
    printf("Name: ");
    puts(name);    //Function to display string.
    return 0;
}
Both, the above program has same output below:
Output
Enter name: Tom Hanks
Name: Tom Hanks

gets() and puts()

Functions gets() and puts() are two string functions to take string input from user and display string respectively


Questions:

(a) main( )
{
char c[2] = "A" ;
printf ( "\n%c", c[0] ) ;
printf ( "\n%s", c ) ;
}
(b) main( )
{
char s[ ] = "Get organised! learn C!!" ;
printf ( "\n%s", &s[2] ) ;
printf ( "\n%s", s ) ;
printf ( "\n%s", &s ) ;
printf ( "\n%c", s[2] ) ;
}
(c) main( )
{
char s[ ] = "No two viruses work similarly" ;
int i = 0 ;
while ( s[i] != 0 )
{
printf ( "\n%c %c", s[i], *( s + i ) ) ;
printf ( "\n%c %c", i[s], *( i + s ) ) ;
i++ ;
}
}


No comments:

Post a Comment