Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Tuesday, 27 December 2011

C program to search and delete a substring from a string



#include<stdio.h>
#include<string.h>
main()
{
char a[50],b[10];
int i,j,temp,p[10]={0},ch,x,pos;
printf("Enter a string\n");
gets(a);
printf("Enter a substring\n");
gets(b);
printf("\n1.Searching\n2.Deletion\nEnter your choice\n");
scanf("%d",&ch);
for(i=0,x=0;i<strlen(a);i++)
{
j=0;
temp=0;
if(a[i]==b[0])
{
pos=i;
while(a[i]==b[j])
{
temp=1;
i++;
j++;
}
}
if((j>=strlen(b))&&(temp==1))
{
p[x]=pos+1;
x++;
}
}
if(ch==1)
{
if(p[0]==0)
printf("\nThe substring not found\n");
else
{
printf("\nSubstring is found at the position:");
for(i=0;i<x;i++)
printf(" %d",p[i]);
}
printf("\n");
exit(0);
}
else if(ch==2)
{
if(p[0]!=0)
{
printf("\nString after deletion\n");
pos=0;
for(x=0;p[x]!=0;x++)
{
for(i=pos;i<p[x]-1;i++)
printf("%c",a[i]);
pos=p[x]-1+strlen(b);
}
for(i=pos;a[i]!='\0';i++)
printf("%c",a[i]);
}
else
printf("\nDeletion is not possible\n");
}
else
printf("\nInvalid choice\n");
printf("\n");
}




Monday, 26 December 2011

C program to sort the given set of strings(Lexicographic sorting)



#include<stdio.h>
#include<string.h>
main()
{
char str[10][20],temp[20];
int i=0,j=0,n;
printf("Enter the number of items\n");
scanf("%d",&n);
printf("Enter the items\n");
while(i<n)
scanf("%s",str[i++]);
for(i=1;i<n;i++)
{
for(j=1;j<=n-i;j++)
{
if(strcmp(str[j-1],str[j])>0)
{
strcpy(temp,str[j-1]);
strcpy(str[j-1],str[j]);
strcpy(str[j],temp);
}
}
}
printf(" \nSorted Items\n");
for(i=0;i<n;i++)
printf(" %s\n",str[i]);
}



Monday, 12 December 2011

C program to find the permutation of a given string using recursion



#include<stdio.h>
#include<string.h>

void perm(char * , int , int );
char temp,str[20];
int l;
int main()
{
printf("Enter the string : ");
scanf("%s",str);
printf("The permutation is\n");
l=strlen(str);
perm(str,0,l);
}

void perm(char *str,const int strt, int len)
{
int i,j;
for(i=strt;i<len-1;++i)
for(j=i+1;j<len;++j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
perm(str , i+1 ,len);
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
printf("%s\n",str);
}



C program to show acromatic string



#include<stdio.h>

main()
{
char ch[100];
int i;
printf("Enter any string : ");
  gets(ch);
i=0;
printf("%c",ch[0]);
while(ch[i]!='\0')
{
if(ch[i]==' ')
{
i++;
printf("%c",ch[i]);
}
i++;
}
printf("\n");
}