Sunday 31 July 2016

String operations using a header file .

Hello Friends!
If there are several functions alike or we can sort them together for that we can create our own header file with extension '.h' and include them in our program as #include"SAMPLE.h" .
So, here are some simple operations performed on the string using that source header file .


//Q1.
// check length

#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];

printf("Enter the string :\n");
gets(str);
int l;
l=length(str);
printf("Length of string is : %d\n",l);
}

//Q2
// Concatenation of two different strings

#include<stdio.h>
#include"STRING.h"
void main()
{
char str1[50],str2[50];
printf("Enter the string 1:\n");
gets(str1);
printf("Enter the string 2:\n");
gets(str2);

conc(str1,str2);
}


//Q3.
//position and occurence of a given symbol in string

#include<stdio.h>

#include"STRING.h"

void main()
{
char str[50],character;
printf("Enter string:\n");
gets(str);
printf("Enter character:\n");
scanf("%c",&character);
pos(str,&character);
}

//Q4
//retriving the last occurence

#include<stdio.h>

#include"STRING.h"

void main()
{
char str[50],character;
printf("Enter string:\n");
gets(str);
printf("Enter character:\n");
scanf("%c",&character);
lastpos(str,&character);
}

//Q5
//retriving the position of substring in main string.

#include<stdio.h>
#include"STRING.h"

void main()
{
char str[50],sub[50];
int position;
printf("Enter string:\n");
gets(str);
printf("Enter substring :\n");
gets(sub);
position=substring(str,sub);
printf("The position of substring is :\t%d\n",position);
}

//Q6
//Replace the given substring in the main string from the given position

#include<stdio.h>
#include"STRING.h"

void main()
{
char str[50],sub[50],rep[50];
int position;
printf("Enter string:\n");
gets(str);
printf("Enter substring to be replaced:\n");
gets(sub);
printf("Enter substring of same lenght:\n");
gets(rep);
position=substring(str,sub);
replace(str,rep,position);
}

//Q7.
//compare strings

#include<stdio.h>
#include"STRING.h"
void main()
{
char str1[50],str2[50];
int ans=0;
printf("Enter two strings to compare :\n");
gets(str1);
gets(str2);
ans = comp(str1,str2);
if(ans)
    printf("the strings are equal\n");
else
    printf("the strings are not equal\n");
}

//Q8.
//compare strings without case sensitivity

#include<stdio.h>
#include"STRING.h"
void main()
{
char str1[50],str2[50];
int ans=0;
printf("Enter two strings to compare :\n");
gets(str1);
gets(str2);
ans = compare(str1,str2);
if(ans)
    printf("the strings are equal\n");
else
    printf("the strings are not equal\n");
}

// q9.
//reverse string

#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
reverse(str);
}

//Q. 10
// upper case
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
up(str);
}

// Q.11
// lower case
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
low(str);
}

//Q.12
// Convert the string to Title Case
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
title(str);
}

//Q 13
//Concatenation of two different strings

#include<stdio.h>
#include"STRING.h"
void main()
{
char str[100];
printf("Enter the string to duplicate and concatenate :\n");
gets(str);
dupconc(str);
}

// Q. 14
// concatenate the reversal of the string
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[100];
printf("Enter the string :\n");
gets(str);
revconc(str);
}

// Q15
// check palindrome
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[100];
int ans=0;
printf("enter string :\nFor checking it is palindrome or not?\n");
gets(str);
ans = check(str);
if(ans)
    printf("Yes ! Its a palindrome\n");
else
    printf("Sorry!\n");

}

// Q.16
// Display and count the vowels present in the string
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
vow(str);
}

//Q17
//Count the characters and words
#include<stdio.h>
#include"STRING.h"
void main()
{
char str[50];
printf("Enter the string:\n");
gets(str);
int no_char,no_words;
no_char=countch(str);
no_words=countw(str);
printf("The number of:\nCharacters : %d\nWords : %d\n",no_char,no_words);
}


Header file : 



// A header file "STRING.h"
// consisting functions
// regarding to string operations .

// 1.length of a string
int length(char *a)
{
int i;
for(i=0;*(a+i)!='\0';i++);
return i;
}

//2. concatenate two strings
void conc(char *a,char *b)
{
int i,j,l;
char c[100];
for(i=0;*(a+i)!='\0';i++)
{
    c[i]= *(a+i);
}
for(j=0;*(b+j)!='\0';j++)
{
    c[i+j]=*(b+j);
}
c[j+i]='\0';
printf("The concatenated string is :\n%s\n",c);
}

//3. position and occurence of the given character in string.

void pos(char *a,char *character)
{
int i,j,position,count=0;
for(i=0;*(a+i)!=*character;i++);
position = i+1;
for(j=0;*(a+j)!='\0';j++)
{
    if(*character == *(a+j))
        count++;
}
printf("The first position is %d\nOccurence is %d\n",position,count);

}

//4. last occurence

void lastpos(char *a,char *character)
{
int i,j,position,count=0;
for(i=0;*(a+i)!='\0';i++)
{
    if( *(a+i)==*character)
        position = i+1;
}
for(j=0;*(a+j)!='\0';j++)
{
    if(*character == *(a+j))
        count++;
}
printf("The last position is %d\nOccurence is %d\n",position,count);

}

//5. position of substring

//retriving the position of substring in main string.

int substring(char *a,char *sub)
{
int i=0,j=0;

while(a[i]!='\0')
{
    if(a[i]==sub[0])
    {    j=1;
        while(sub[j]!='\0' && a[i+j]!='\0' && a[i+j]==sub[j] )
        {
            j++;
        }
    }
    if(sub[j]=='\0')
        return i+1;
i++;
}

return 0;
}


//6. replacing substring

void replace(char *a,char *rep,int p)
{
int i,j;
for(i= p-1,j=0;rep[j] != '\0';i++,j++)
{
    a[i]=rep[j];
}
printf("The new string is :\n%s\n",a);
}

//7. comparing two strings

int comp(char *a,char *b)
{
int i=0,j=0,k;
while(*(a+i)==*(b+i) && *(a+i)!='\0' && *(b+i)!='\0')
{
    i++;
}
if(*(a+i)=='\0' && *(b+i)=='\0')
    return 1;
else
    return 0;
}

//8. comparing two strings without case sensitivity

int compare(char *s1,char *s2)
{
int i ,j=0;
for( i=0;*(s1+i)!='\0' && *(s2+i)!='\0';i++)
{
    if(s1[i]==s2[i])
        j++;
    else if(s1[i]+32==s2[i])
        j++;
    else if(s1[i]-32==s2[i])
        j++;
    else
        return 0;
}
return 1;
}

//9. Reverse the given string !

void reverse(char *a)
{
int i,j,k;
char r[50];
for(i=0;a[i]!='\0';i++);
for(j=i-1,k=0;j>=0;j--,k++)
{
    r[k]=a[j];
}
r[k]='\0';

printf("the reverse string is :\n%s \n",r);
}

//10. convert the string to upper case

void up(char *a)
{
int i;
for( i=0;a[i]!='\0';i++)
{
    if(a[i]>=97 && a[i]<=123)
        a[i]=a[i]-32;
}
printf("The required string is :\n%s\n",a);
}

//11. convert the string to lower case

void low(char *a)
{
int i;
for( i=0;a[i]!='\0';i++)
{
    if(a[i]>=65 && a[i]<=91)
        a[i]=a[i]+32;
}
printf("The required string is :\n%s\n",a);
}

//12. Convert the string to Title Case

void title(char *a)
{
int i;
if(a[0]>=97 && a[0]<=122)
    a[0]=a[0]-32;
for( i=0;a[i]!='\0';i++)
{
    if(a[i]==' ')
    {
        if(a[i+1]>=97 && a[i+1]<=122)
            a[i+1]=a[i+1]-32;
    }
}
printf("The required string is :\n%s\n",a);
}

// 13.duplicate and concatenate two strings
void dupconc(char *a)
{
int i,j,l;
char dup[50];
for(i=0;*(a+i)!='\0';i++)
{
    dup[i]= *(a+i);
}
dup[i]='\0';
conc(a,dup);
}

// 14. reverse and concatenate two strings
void revconc(char *a)
{
int i,j,k;
char r[50];
for(i=0;a[i]!='\0';i++);
for(j=i-1,k=0;j>=0;j--,k++)
{
    r[k]=a[j];
}
r[k]='\0';
conc(a,r);
}

// 15. check palindrome
int check(char *a)
{
int i,j,k,n,m=0,len;
char r[50];
for(i=0;a[i]!='\0';i++);
len=i;
for(j=i-1,k=0;j>=0;j--,k++)
{
    r[k]=a[j];
}
r[k]='\0';
for(n=0;a[n]!='\0' && r[n]!='\0';n++)
{
    if(a[n]==r[n])
    {
    m++;
    }
}
if(m==len)
    return 1;
else
    return 0;
}

//16. Display and count the vowels present in the string
void vow(char *a)
{
int i,j,count=0;
printf("The vowels are :\n");
for(i=0;a[i]!='\0';i++)
{
    if(a[i]=='a' || a[i]=='A' || a[i]=='e' || a[i]=='E' || a[i]=='i' || a[i]=='I' || a[i]=='o' || a[i]=='O' || a[i]=='u' || a[i]=='U'){
    printf("%c,",a[i]);
    count++;
    }
}
printf("Number of vowels are :\n%d\n",count);
}

//17. count characters and words
//17.1 counting characters
//white space is excluded.
int countch(char *a)
{
int i,j,count=0;
for(i=0;a[i]!='\0';i++)
{
    if(a[i]!=' '){
    count++;
    }
}
return count;
}

//17.2 counting words
//it is assumed that words are seperated by white spaces
int countw(char *a)
{
int i,j,count=1;
for(i=0;a[i]!='\0';i++)
{
    if(a[i]==' '){
    count++;
    }
}
return count;
}


Thanking you !


No comments :

Post a Comment