15.7 – Programs on strings part – 2

by subbu on August 26, 2014

Specification: Accept the line of text and print the sum of all the digits given in the text

Program:

#include<stdio.h>
int main()
{
char x[80];
int sum=0,i;
printf("Enter the text:\n");
gets(x);
for(i=0;x[i]!='\0';i++)
{
  if(x[i]>='0'&&x[i]<='9') /* checking for a digit */
    sum=sum+(x[i]-'0');   /* x[i]-'0' converts character digit into numeric digit */
}
printf("Sum of all the digits %d",sum);
return 0;
}

Execution:
Enter the text:
hello 123
Sum of all the digits 6

Specification: Accept any string and print the most repeated characters

Program:

#include<stdio.h>
int main()
{
char x[80];
int cc[127]={0},i,cidx;
int maxidx,max;
printf("Enter a line of text:");
gets(x);
for(i=0;x[i]!='\0';i++)
{
  cidx=x[i];                /* Assigns the ASCII value to index */
  cc[cidx]++;     /* increments related element in counter array */
}
for(max=cc[0],i=0;i<127;i++)
{
 if(cc[i]>max)
 {
   max=cc[i];
   maxidx=i;
 }
}
printf("Most frequent character %c is existed for %d times\n",maxidx,max);
printf("\n");
return 0;
}

Execution:
Enter a line of text:codingfox.com
Most frequent character o is existed for 3 times

Specification: Accept any two strings and print whether they are Anagrams

Logic
The strings are said to be anagrams of each other, if the letters from one sting can be rearranged to form the other string. From the above definition it is also clear that two strings are anagrams if all characters in both strings occur same number of times

Program:

#include<stdio.h>
int main()
{
char x[80],y[80];
int ccx[26]={0},ccy[26]={0},i,idx,flag;  /* arrays with 0 elements to count the characters */
printf("First string:");
scanf("%s",x);
printf("Second string:");
scanf("%s",y);
/* counting characters of first string */
for(i=0;x[i]!='\0';i++)
{
 idx=x[i]-'a';  /* Index at which count array element need to be incremented */
 ccx[idx]++;    /* incrementing count array element of related character     */
}
/* counting characters of second string */
for(i=0;y[i]!='\0';i++)
{
 idx=y[i]-'a';
 ccy[idx]++;
}
/* comparing two counting arrays for inequality */
for(flag=1,i=0;i<26;i++)
if(ccx[i]!=ccy[i])
{
 flag=0;
 break;
}
if(flag)
 printf("Both strings are Anagrams");
else
 printf("Both are not anagrams");
return 0;
}

Execution
First string:india
Second string:idian
Both strings are Anagrams

Specification: Accept any line of text, convert it into an array of strings and print the resultant array of strings

Program:

#include<stdio.h>
int main()
{
char x[80];
char y[20][20];
int i,r,c;
printf("Give the text:\n");
gets(x);
r=0,c=0;
for(i=0;x[i]!='\0';i++) /* traversing string */
{
 if(x[i]!=' ')      /* in case of character in the text */
 {
   y[r]1=x[i];    /* is stored in the matrix */
   c++;             /* cloumn is changed       */
 }
 else               /* in case of \0 in the text        */
 {
  y[r]1='\0';    /* String is terminated    */
  r++;             /* Row is incremented by 1 */
  c=0;             /* Column is set to 0 */
 }
}
printf("The words in the Array of strings:");
for(i=0;i<=r;i++)
  printf("\n%s",y[i]);
return 0;
}

Execution:
Give the text:
hello this is igate solutions
The words in the Array of strings:
hello
this
is
igate
solutions

Specification: Program to accept any string and remove a given word from the string

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[80];
char y[20][20];
char f[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++) /* traversing string */
{
 if(x[i]!=' ')       /* in case of character in the text */
 {
   y[r]1=x[i];   /* is stored in the matrix */
   c++;            /* cloumn is changed       */
 }
 else                /* in case of \0 in the text        */
 {
   y[r]1='\0';   /* String is terminated    */
   r++;            /* Row is incremented by 1 */
   c=0;            /* Column is set to 0 */
 }
}
wcount=r+1;             /* number of words */
printf("Enter the word to remove:");
scanf("%s",f);
/* Removing the word from array of strings */
for(i=0;i<wcount;i++)
 if(strcmp(y[i],f)==0)       /* Searching for word */
 {
   for(;i<wcount-1;i++)     /* Removing the word  */
     strcpy(y[i],y[i+1]);
    break;
 }
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);
return 0;
}

Execution:
Give the text:Hello this is great codingfox.com
Enter the word to remove:great
The resultant text:Hello this is codingfox.com

Specification: Accept a line of text and reverse every word

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[80];
char y[20][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;
/* Reversing every word in the array of strings */
for(i=0;i<wcount;i++)
   strrev(y[i]);
/* 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 codingfox.com
The resultant text:olleh siht si moc.xofgnidoc

Specification: Accept lines of text and print all the lines containing a given string

Program:

#include<stdio.h>
#include<string.h>
int search(char*,char*);
int main()
{
char x[20][80];
char f[20],t[80];
int i,lcount;
printf("Enter lines of text until exit:\n");
i=0;
for(i=0;;i++)
{
 gets(t);
 if(strcmp(t,"exit")==0)
   break;
 strcpy(x[i],t);
}
lcount=i;
printf("Enter the string to search:");
scanf("%s",f);
printf("The lines with given words:");
for(i=0;i<lcount;i++)
 if(search(x[i],f))
   printf("\n%s",x[i]);
return 0;
}
int search(char *s,char *f)
{
int loftxt,lofwrd,k,i,j,flag;
loftxt=strlen(s);
lofwrd=strlen(f);
for(k=0;k<loftxt;k++)        /* setting a place in the text to search for a word */
{
  for(flag=1,i=k,j=0;j<lofwrd;i++,j++) /* comparing word with text from setted place */
   if(s[i]!=f[j])                     /* checking for inequality */
   {
     flag=0;
     break;
   }
    if(flag)
     return 1;                   /* search successful */
}
return 0;
}

Execution:
Enter lines of text until exit:
hello world
polo world
hello igate
igate solutions
codingfox
exit

Enter the string to search:igate
The lines with given words:
hello igate
igate solutions

Specification: Convert a string into integer and integer into string

Program to convert string to int

#include<stdio.h>
int str2int(char*);
int main()
{
char x[50];
int n;
printf("Enter a string:");
scanf("%s",x);
n=str2int(x);
printf("Equal number: %d",n);
return 0;
}
int str2int(char *s)
{
int i,n;
for(n=0,i=0;s[i]!='\0';i++)    /* extracting characters */
  n=n*10+(s[i]-'0');                   /* converting and concatenating to number */
return n;
}

Execution:
Enter a string:5467
Equal number: 5467

Program to convert an integer into string

#include<stdio.h>
void int2str(char*,int);
int main()
{
char x[50];
int n;
printf("Enter Integer number:");
scanf("%d",&n);
int2str(x,n);
printf("Equal string: %s",x);
return 0;
}
void int2str(char *s,int n)
{
int temp,rev,i;
/* reversing the number */
for(rev=0,temp=n;temp!=0;temp=temp/10)
  rev=rev*10+(temp%10);
/*extracting digits and adding to string */
for(i=0;rev!=0;rev=rev/10,i++)
   s[i]='0'+rev%10;
s[i]='\0';
}

Execution:
Enter Integer number:6745
Equal string: 6745

Previous post:

Next post: