Saturday, 11 February 2012

String Concatenation in C


String Concatenation in C

#include<stdio.h>
#include<string.h>
void main()
{
char str1[10],str2[10],str[20];
int i=0,j=0,k=0;
clrscr();
printf ("enter the two strings\n");
gets (str1);
gets (str2);
while (str1[i]!='\0')
{
str[k++]=str1[i++];
}
while (str2[j]!='\0')
{
str[k++]=str2[j++];
}
str[k]='\0';
printf("The concatenated string is:");
puts (str);
getch();
}

Program to Compare Two Strings in C

 Program to Compare Two Strings in C

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string ");
gets(a);
printf("Enter the second string ");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
getch();
return 0;
}

C Program to show no of occurrences of a string in a paragraph


C Program to show no of occurrences of a string in a paragraph



#include<stdio.h>
#include <string.h>
void main()
{
char st1[25],st2[25];
int cnt,i,j,k,c,len,m,sign;
clrscr();
printf("Enter the first string\n");
gets(st1);
printf("Enter the second string\n");
gets(st2);
len=strlen(st1);
for(i=0;i<len;i++)
{
c=0;
if (st1[i]==st2[c])
{
m=i;
sign=0;
cnt=0;
while(st2[c]!='\0' && sign!=1)
{
if (st1[m]==st2[c])
{
m++;c++;
cnt++;
}
else
sign=1;
}
if (sign==0)
{
printf("The given string is present\n");
printf("The starting position %d & ending position %d\n",i+1,(i+cnt));
k=1;
}
}
}
if (k != 1)
if (sign!=0)
printf("The given string is not present\n");
getch();
}