Thursday, 11 September 2014

String operations in C Language with example program


String operations in C with an example program is available here. Given C program is based on basic manipulations on strings. Simple source code is given below.

BASIC OPERATIONS ON STRING IN C
Basic string operations in C program is used to perform the operations like finding the length of a string, concatenation of two strings, comparison between two strings and copy the string value. All these functions are performed by this program using string function.

String manipulations program:

Q: Write a C program to manipulate the strings with an example programs.

//Header files
#include<stdio.h>
#include<conio.h>
 
//Global variables
int xstrlen(char*); //Finding length of string function
void xstrcat(char*,char*); //Concatenation of two string function
int xstrcmp(char*,char*); //comparison of two string function
char *xstrcpy(char*,char*); //copy of string value function
 
void main()
{
 
//Program variables
char *st1,*st2,*st3;
int leng,l;
clrscr(); //Function to clear previous function
 
puts("enter the first string:"); //Display function
gets(st1); //Getting input function
puts("enter the second string:");
gets(st2);
 
//leng=xstrlen(st1);
//xstrcat(st1,st2);
//s3=xstrcpy(st1,st2);
l=xstrcmp(st1,st2);
if(l==0//Conditional statement
printf("Strings are similar");
else
printf("Strings are not similar");
//printf("\n String concatentation is%s:",st1);
//printf("\n The length of string%d",len);
getch();
}
 
int xstrlen(char*s)
{
int l=0;
while(*s)
{
    l++;
    s++;
}
return l;
}
 
void xstrcat(char *st4,char *st5)
{
char*a=st4;
while(*st4)
{
    st4++;
}
while(*st5!='\0'//Unconditional statement
{
    *st4=*st5;
    st4++;
    st5++;
}
*st4='\0';
printf("Two stringsare%s",a);
}
 
char *xstrcpy(char *st6,char *st7)
{
char *b=st6;
    while(*st6)
    {
        *st7=*st6;
        st7++;
        st6++;
    }
*st7='\0';
return b;
printf("the copied string %s",b);
}
 
int xstrcmp(char *st8,char *st9)
{
    while(*st8==*st9)
    {   if(*st8==NULL)
 
        return 0;
        st8++;
        st9++;
 
    }
      return(*st8-*st9);
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.