Assignment 5: String Handling                                                    Date : 12th  Feb, 07

                  Do not use any standard string handling functions.


Introduction:

 

Arrays:

An array is a variable that holds multiple values of the same type. Arrays can be created from any of the C data types like int, float, char,... The contents of an array can be accessed by its index value. In C, an array index starts from zero. The elements of the array occupy adjacent locations in memory.

The syntax for declaring an array is :

                                     type name[size];

 

Character Arrays:

 

 An array of characters should always terminated by the special null character ‘\0’. For example a string constant, such as "SPRING" is an array of characters. It is represented internally by the ASCII characters as

                                         ‘S’, ‘P’, ‘R’, ‘I’, ‘N’, ‘G’, ‘\0’.

 
A small example depicting the declaration of arrays is shown below.
 

                                                  #include<stdio.h>

                                         int  main (void) {
 
                                                   
                                                   int i=0;
                                                                                                            
                                                   int x[4],y[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};// declaring an integer array
 
                                                   char Letter='x';
            
                                                   char Letters[10] = {'c','h','a','r','a','c','t','e','r','\0'}; //declaration of a character array
 
                                                   char Text[10]="character"; // alternative way of declaring a character array
 
                                                   for(i=0;i<4;i++) 
                                                    {
                                                       scanf(“%d”,&x[i]);
                                                    }
                                                 
                                                   for(i=0;i<4;i++)  
                                                    {
                                                       printf("\n The value of x[%d] is %d",i, x[i]);
                                                    }
 
                                                   printf("\n The value of y[0] is %d", y[0]);
 
                                                   printf("\n The value of y[9] is %d", y[9]);
 
                                                   printf(“\n The string Letters is %s and the string Text is %s ”, Letters,Text);
 
                                                 }
 
INPUT:
 
             0,1,2,3.
 
OUTPUT:
 
             The value of x[0] is 0
 
             The value of x[1] is 1
 
             The value of x[2] is 2
 
             The value of x[3] is 3
 
             The value of y[0] is 1
 
             The value of y[9] is 10
 
             The string Letters is character and the string Text is character

Tutorial 5A:

Write a C program to find the length of a given string.

Solution:

#include<stdio.h>

#define MAXLEN 100

int  main(){

int i,length;

char str[MAXLEN];

           printf(“Enter the input String.:”);

           scanf(“%[^\n]s”,str);

           for(i=0,length=0;str[i]!=’\0’;i++)

                 length++;

           printf(“length of given String is %d”,length);

           }

Example:

INPUT

OUTPUT

Hello World

11

!1234$

6



Assign. 5A:

Write a C program to check whether given strings are identical or not.

INPUTS

OUTPUT

Computer, Computer

Equal

Programming, Programing

Not equal



Tutorial 5B:

    Write a C program to concatenate two strings.

Solution:

#include<stdio.h>

#define MAXLEN 100

int main(){

int i,c;

char str1[MAXLEN],str2[MAXLEN],str3[MAXLEN];

printf(“Enter the first input String.:”);

scanf(“%[^\n]s”,str1);

printf(“Enter the second input String.:”);

scanf(“%[^\n]s”,str2);

for( i = 0 , c = 0 ; str1[i]!=’\0’ ; i++ )

     {

      str3[c] = str1[i];

      c++;

     }

for( i = 0 ; str2[i]!=’\0’ ; i++ )

     {

      str3[c] = str2[i];

      c++;

     }

str3[c]=’\0’;

                                     printf(“Concatenated String is :%s”,str3);

                                           }

Example:

INPUTS

OUTPUT

Compute, r

Computer

Prog  ram, ming

Prog  ramming



Assignment 5B:

    Write a C program to extract a particular portion of a string. The positions (starting and ending) of extraction will be given as inputs.

INPUTS

OUTPUT

Computer, 3, 5

mpu

Programming, 1, 21

Invalid input



Tutorial 5C:

    Write a C program to replace a particular character in a string with a given character.

Solution:

#include<stdio.h>

#include<stdlib.h>

#define MAXLEN 100

int main(){

int i;

char c,d,str[MAXLEN];

printf(“Enter the first input String.:”);

scanf(“%[^\n]s”,str);

printf(“Enter the character to be replaced.:”);

scanf(“%c”,&c);

printf(“Enter the character with which to be replaced.:”);

flushall();

scanf(“%c”,&d);

for( i = 0  ; str[i]!=’\0’ ; i++ )

     {

      if( str[i] == c)

      str[i] = d;

      }

                                     printf(“Resultant String is :%s”,str);

                                           }  

INPUTS

OUTPUT

Cartoon, o, a

Cartaan

Programming, g, k

Prokrammink



Assign 5C:

      Write a C program to remove a particular character in a string.

INPUTS

OUTPUT

Computer, p

Comuter

Programming, m

Prograing



Assign. 5D:

    Write a C program to check whether a given word is palindrome or not.

       Ex: MALAYALAM, ANNA, AMANAPLANACANALPANAMA…

Hint: A word is said to be palindrome if it reads the same backward or forward.