15.8 – Programs on Strings Part – 3

by subbu on September 1, 2014

Specification: Insert a word in the string

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[80],y[20];
int pos,wlen,slen,i,j;
printf("Enter a line of text:");
gets(x);
printf("Enter the position:");
scanf("%d",&pos);
printf("Enter the word to insert:");
scanf("%s",y);
slen=strlen(x);
wlen=strlen(y);
strcat(y," ");   /* Adding a space at the end of word */
/* Shifting all the characters from the position by word length +1 */
for(i=slen;i>=pos;i--)
  x[i+wlen+1]=x[i];
/* Copying word on to string */
for(i++,j=0;y[j]!='\0';j++,i++)
  x[i]=y[j];
printf("Resultant string %s",x);
return 0;
}

Execution:
Enter a line of text:hello world
Enter the position:6
Enter the word to insert:igate
Resultant string hello igate world

Specification: Accept a string and print the words that end with “e”

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[80],y[20];
int i,j;
printf("Enter any string:");
gets(x);
for(i=0,j=0;x[i]!='\0';i++)
{
  y[j]=x[i]; /* copying word into y */
  if(x[i]=='e'&&(x[i+1]==' '||x[i+1]=='\0')) /* if last character of word is 'e' */
  {
    y[j+1]='\0';
    printf("\n%s",y);      /* printing the word in y */
    j=0;                /* resetting y            */
    i++;
  }
  else if(x[i]==' ')      /* going to the next word */
     j=0;                /* resetting y  */
  else
     j++;
}
return 0;
}

Execution:
Enter any string:C the complete reference
the
complete
reference

Specification: Accept a line of text, find and replace a word within the text

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[80];
char y[20][20];
char fnd[20],rpl[20];
int i,r,c,wcount;
printf("Give the text:");
gets(x);
/* Converting the text into array of strings */
r=0,c=0;
for(i=0;x[i]!='\0';i++)
{
  if(x[i]!=' ')
  {
    y[r]1=x[i];
    c++;
  }
  else
  {
    y[r]1='\0';
    r++;
    c=0;
  }
}
wcount=r+1;
printf("Find what?");
scanf("%s",fnd);
printf("Replace with what?");
scanf("%s",rpl);
for(i=0;i<wcount;i++)
 if(strcmp(y[i],fnd)==0)    /* replacing word */
   strcpy(y[i],rpl);
/* Converting string array to text */
for(i=0,r=0;r<wcount;r++,i++)
{
 for(c=0;y[r]1!='\0';c++,i++)
    x[i]=y[r]1;
 x[i]=' ';
}
x[i]='\0';
printf("The resultant text:%s",x);
return 0;
}

Execution:
Give the text:hello this is igate solutions
Find what?igate
Replace with what?codingfox
The resultant text:hello this is codingfox solutions

Specification: Accept a line of text and print the words in sorted order

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[80];
char y[20][20],temp[20];
int i,j,c,r,wcount;
printf("Give the text:");
gets(x);
/* Converting the text into array of strings */
r=0,c=0;
for(i=0;x[i]!='\0';i++)
{
  if(x[i]!=' ')
  {
     y[r]1=x[i];
     c++;
  }
  else
  {
    y[r]1='\0';
    r++;
    c=0;
  }
}
wcount=r+1;
/* sorting the words */
for(i=0;i<wcount-1;i++)
 for(j=i+1;j<wcount;j++)
 {
   if(strcmp(y[j],y[i])<0)
   {
     strcpy(temp,y[i]);
     strcpy(y[i],y[j]);
     strcpy(y[j],temp);
   }
 }
/* Converting string array to text */
for(i=0,r=0;r<wcount;r++,i++)
{
 for(c=0;y[r]1!='\0';c++,i++)
     x[i]=y[r]1;
  x[i]=' ';
}
x[i]='\0';
printf("The resultant text:%s",x);
return 0;
}

Execution:
Give the text:igate solutions computer education
The resultant text:computer education igate solutions

Specification: Accept a line of text and remove all the repeated words and print the number of unique words

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[80];
char y[20][20];
int i,j,c,r,wcount,k;
printf("Give the text:");
gets(x);
/* Converting the text into array of strings */
r=0,c=0;
for(i=0;x[i]!='\0';i++)
{
 if(x[i]!=' ')
 {
   y[r]1=x[i];
   c++;
 }
 else
 {
   y[r]1='\0';
   r++;
   c=0;
 }
}
wcount=r+1;
/* removing the repeated words */
for(i=0;i<wcount-1;i++)
{
 for(j=i+1;j<wcount;j++)
  if(strcmp(y[i],y[j])==0)
  {
    for(k=j;k<wcount-1;k++)
      strcpy(y[k],y[k+1]);
    wcount--;
  }
}
/* Converting string array to text */
for(i=0,r=0;r<wcount;r++,i++)
{
  for(c=0;y[r]1!='\0';c++,i++)
     x[i]=y[r]1;
  x[i]=' ';
}
x[i]='\0';
printf("The resultant text:%s",x);
printf("\nThe number of unique words are %d",wcount);
return 0;
}

Execution:
Give the text:igate solutions igate computer education education
The resultant text:igate solutions computer education
The number of unique words are 4.

Specification: Accept a line of text and print the smallest, biggest words according to the Dictionary order

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[80];
char y[20][20],temp[20];
int i,j,c,r,wcount;
printf("Give the text:");
gets(x);
/* Converting the text into array of strings */
r=0,c=0;
for(i=0;x[i]!='\0';i++)
{
  if(x[i]!=' ')
  {
    y[r]1=x[i];
    c++;
  }
  else
  {
    y[r]1='\0';
    r++;
    c=0;
  }
}
wcount=r+1;
/* sorting the words */
for(i=0;i<wcount-1;i++)
 for(j=i+1;j<wcount;j++)
 {
   if(strcmp(y[j],y[i])<0)
   {
     strcpy(temp,y[i]);
     strcpy(y[i],y[j]);
     strcpy(y[j],temp);
   }
}
printf("The smallest word %s",y[0]);
printf("\nThe biggest word %s",y[wcount-1]);
return 0;
}

Execution:
Give the text: igate solutions computer education
The smallest word computer
The biggest word solutions

Previous post:

Next post: