Tuesday, September 24, 2013

HANDLING OF STRINGS

HANDLING OF STRINGS

In C programming, array of character are called strings.

A string is terminated by null character /0. For example:

"My course BCA"
Here, "My course  BCA " is a string. When, compiler encounters strings, it appends null character at the end of string.

Declaration of strings

Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type.

char s[5];

Declaration of strings in C language

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'};

Initialization of strings in C programming

String can also be initialized using pointers

char *c="abcd";
Reading Strings from user.
Reading words from user.
char c[20];
scanf("%s",c);
String variable c can only take a word. It is because 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: king mcb
Your name is king

Here, program will ignore mcb because, scanf() function takes only string before the white space.

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: king  mcb
Name: king mcb

Passing Strings to Functions

String can be passed to function in similar manner as arrays as, string is also an array.

#include <stdio.h>
void Display(char ch[]);
int main(){
    char c[50];
    printf("Enter string: ");
    gets(c);            
    Display(c);     // Passing string c to function.  
    return 0;
}
void Display(char ch[]){
    printf("String Output: ");
    puts(ch);
}

Here, string c is passed from main() function to user-defined function Display().

In function declaration, ch[] is the formal argument.

String handling functions

You can perform different type of string operations manually like: finding length of string, concatenating(joining) two strings etc.

 But, for programmers ease, many library function are defined under header file <string.h> to handle these commonly used talk in C programming.

String Manipulations In C Programming Using Library Functions

Strings are often needed to be manipulated by programmer according to the need of a problem.

All string manipulation can be done manually by the programmer but, this makes programming complex and large.


To solve this, the C supports a large number of string handling functions.

There are numerous functions defined in "string.h" header file. Few commonly used string handling functions are discussed below:


strlen() Calculates the length of string
strcpy() Copies a string to another string
strcat() Concatenates(joins) two strings
strcmp() Compares two string
strlwr() Converts string to lowercase
strupr() Converts string to uppercase

Strings handling functions are defined under "string.h" header file, i.e, you have to include the code below to run string handling functions.

#include <string.h>


gets() and puts()

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

#include<stdio.h>
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;
}
Though, gets() and puts() function handle string, both these functions are defined in "stdio.h" header file.

No comments:

Post a Comment