15.6 – Programs on strings part – 1

by subbu on August 20, 2014

Specification: Accept any string and print whether the string is a palindrome string or not.

Logic:
If the reverse of a string is the same then the string is called a palindrome string.
Examples: liril, madam, malayalam, wow etc.

Program:

#include<stdio.h>
#include<string.h>
int main()
{
char x[50],y[50];
printf("Enter any string:");
scanf("%s",x);
strcpy(y,x);       /* "x" is copied on to "y" */
strrev(x);           /* "x" is reversed */
if(strcmp(x,y)==0)    /* comparing the given and its reverse string */
 printf("Palindrome string");
else
 printf("Not a palindrome string");
return 0;
}

Execution1:
Enter any string: liril
Palindrome string

Execution2:
Enter any string: madan
Not a palindrome string

Note: The above program may not work with gcc because strrev() is not available with gcc

Specification: Accept any string and print whether the string is a palindrome string or not without using library functions

Logic:
We compare two characters from the beginning and ending towards middle. If we find any inequality then the given string is not a palindrome string otherwise it is a palindrome string.

palidrome string or not

Program:

#include<stdio.h>
int main()
{
char x[50];
int i,j,flag;
printf("Enter any string:");
scanf("%s",x);
for(j=0;x[j]!='\0';j++);    /* to send the "j" to the end of string '\0' */
j--;                                   /* to set the "j" before end of string */
flag=1;                            /* default value of flag is 1 */
for(i=0;i<j;i++,j--)
 if(x[i]!=x[j])              /* checking for inequality */
 {
  flag=0;                  /* flag is stored with zero if inequality is found */
  break;
 }
if(flag)
 printf("Palindrome string");
else
 printf("Not a palindrome string");
return 0;
}

Execution1:
Enter any string: madam
Palindrome string

Execution2:
Enter any string: madan
Not a palindrome string

Specification: Accept the details of an employee and print the total address.

Program:

#include<stdio.h>
int main()
{
char name[50],street[50],city[50];
long int pin;
printf("Name:");
scanf("%s",name);
printf("Street:");
scanf("%s",street);
printf("City:");
scanf("%s",city);
printf("Pin code:");
scanf("%ld",&pin);
printf("Address:\n");
printf("Name %s\nStreet %s\nCity %s\nPin code %ld",name,street,city,pin);
return 0;
}

Execution:
Name: subbu
Street: Ko-Street
City: Hawai
Pin code: 89675

Address:
Name subbu
Street Ko Street
City Hawai
Pin code 89675

Specification: Accept the marks of a student in 3 subjects and print the total, average and the result.

program:

#include<stdio.h>
#include<string.h>
int main()
{
int m1,m2,m3,tot;
float avg;
char result[50];
printf("Enter the marks in 3 subjects:\n");
scanf("%d%d%d",&m1,&m2,&m3);
tot=m1+m2+m3;
avg=(float)tot/3;
if(m1<35||m2<35||m3<35)
 result="Failed";
else if(avg>=75)
 result="Distiction";
else if(avg>=60)
 result="First class";
else if(avg>=50)
 result="Second class";
else
 result="Ordinary";
printf("Total:%d\nAverage:%f\nResult:%s",tot,avg,result);
return 0;
}

Output:
Error: LValue required in function main()

Note: Because string is a character array, we can’t directly assign a string constant to the character array rather we need to use strcpy() to assign a string to the character array.

Program rectified:

#include<stdio.h>
#include<string.h>
int main()
{
int m1,m2,m3,tot;
float avg;
char result[50];
printf("Enter the marks in 3 subjects:\n");
scanf("%d%d%d",&m1,&m2,&m3);
tot=m1+m2+m3;
avg=(float)tot/3;
if(m1<35||m2<35||m3<35)
  strcpy(result,"Failed");
else if(avg>=75)
  strcpy(result,"Distinction");
else if(avg>=60)
  strcpy(result,"First class");
else if(avg>=50)
  strcpy(result,"Second class");
else
  strcpy(result,"Ordinary");
printf("Total:%d\nAverage:%f\nResult:%s",tot,avg,result);
return 0;
}

Execution1:
Enter the marks in 3 subjects:
45      67      89
Total: 201
Average: 67.000000
Result: First class

Execution2:
Enter the marks in 3 subjects:
12      55      67
Total: 134
Average: 44.666668
Result: Failed

Specification: Accept a line of text and replace lower characters by upper case and upper case characters by lower case and print the resultant string

Program:

#include<stdio.h>
int main()
{
char x[100];
int i;
printf("Enter a line of text:\n");
gets(x);
for(i=0;x[i]!='\0';i++)
{
 if(x[i]>='A'&&x[i]<='Z') /* Checking of uppercase  */
  x[i]=x[i]+32;       /* Changing to lowercase */
 else if(x[i]>='a'&&x[i]<='z') /* Checking for lowercase */
  x[i]=x[i]-32;       /* Converting into uppercase */
}
printf("The resultant text is:%s",x);
return 0;
}

Execution:
Enter a line of text:
Hello World This is Coding Fox
The resultant text is:hELLO wORLD tHIS IS cODING fOX

Specification: Accept any string and print the sum of ASCII values of all the characters in a string

Program:

#include<stdio.h>
int main()
{
char x[100];
int i,sum;
printf("Enter a line of text:\n");
gets(x);
for(sum=0,i=0;x[i]!='\0';i++)
{
 printf("ASCII of %c is %d\n",x[i],x[i]); /* %d prints the ASCII value */
 sum=sum+(int)x[i];                       /* (int) converts character to ASCII */
}
printf("Sum of ASCII values %d",sum);
return 0;
}

Execution:
Enter a line of text:
Hello World
ASCII of H is 72
ASCII of e is 101
ASCII of l is 108
ASCII of l is 108
ASCII of o is 111
ASCII of   is 32
ASCII of W is 87
ASCII of o is 111
ASCII of r is 114
ASCII of l is 108
ASCII of d is 100
Sum of ASCII values 1052

Specification: Find the number of words in a sentence

Program:

#include<stdio.h>
int main()
{
char x[80];
int spc=0,i;
printf("Enter a line of text:\n");
gets(x);
for(i=0;x[i]!='\0';i++)
{
 if(x[i]==' '&&x[i+1]!=' ') /* Two adjuscent spaces should not consider as two words */
   spc++;            /* counting word spaces */
}
spc++;                      /* words are 1 more than the number of spaces */
printf("Number of words %d",spc);
return 0;
}

Execution1:
Enter a line of text:
hello world
Number of words 2

Excecution2:
Enter a line of text:
hello   world of computers
Number of words 4

Specification: Accept a line of text and print whether a word is existed or not (Searching for a word)

Program:

#include<stdio.h>
int main()
{
char x[80],y[20];
int loftxt,lofwrd;
int i,j,k,flag;
printf("Enter a line of text:\n");
gets(x);
printf("Enter a word to search:");
scanf("%s",y);
/* calculating the length of text */
for(loftxt=0,i=0;x[i]!='\0';i++)
   loftxt++;
/* calculating the length of word */
for(lofwrd=0,i=0;y[i]!='\0';i++)
  lofwrd++;
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(x[i]!=y[j])                     /* checking for unequality */
   {
     flag=0;
     break;
   }
 if(flag)                             /* search successfull */
   break;
}
if(flag)
 printf("Existed");
else
 printf("Not Existed");
return 0;
}

Execution1:
Enter a line of text:
hello this is igate computer world
Enter a word to search:computer
Existed

Execution2:
Enter a line of text:
hello world
Enter a word to search:hell
Existed

Execution3:
Enter a line of text:
hello world
Enter a word to search:polo
Not Existed

Specification: Accept a line of text and find how many times a word is existed

Program:

#include<stdio.h>
int main()
{
char x[80],y[20];
int loftxt,lofwrd;
int i,j,k,flag,count;
printf("Enter a line of text:\n");
gets(x);
printf("Enter a word to search:");
scanf("%s",y);
/* calculating the length of text */
for(loftxt=0,i=0;x[i]!='\0';i++)
  loftxt++;
/* calculating the length of word */
for(lofwrd=0,i=0;y[i]!='\0';i++)
  lofwrd++;
count=0;
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(x[i]!=y[j])                     /* checking for unequality */
  {
    flag=0;
    break;
  }
 if(flag)                             /* search successfull */
   count++;
}
if(count==0)
  printf("Not Existed");
else
  printf("%d times",count);
return 0;
}

Execution1:
Enter a line of text:
hello world hello igate
Enter a word to search: hello
2 times

Previous post:

Next post: