Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

64.strings :strcat(), strcmp(), Accepting a line of text

 

strcat():

In the above example we have developed a function called concat() to concatenate a string to another.

strcat() is a predefined function defined within the header file "string.h" used to concatenate a string to another.

It accepts the addresses of target and source strings as arguments and concatenates the source string to the target string.

 

Function definition:

void strcat(char *p,char *q)

{

    -------------------------

    -------------------------

    -------------------------

}

 

Example:

#include<stdio.h>

#include<string.h>

void main()

{

   char x[50],y[50];

   clrscr();

   printf("Enter the 1st string:");

   scanf("%s",x);

   printf("Enter the 2nd string:");

   scanf("%s",y);

   strcat(x,y);

   printf("The resultant string is %s",x);

   getch();

}

 

Execution:

Enter the 1st string: hello

Enter the 2nd string: india

The resultant string is helloindia

 

Example explained:

strcat() concatenates the source string (y) to target string (x)

 

Specification8:

Accept any two strings and print the biggest string according to the dictionary order using the function concat().

 

Logic to compare two strings:

Function must return the ASCII difference among the first occurrence of un-equal characters.

If it returns the +ve value then the first string is the biggest one.

If it returns the –ve value then the second string is the biggest one.

If it returns 0 then both the strings are equal.

 

Different cases:

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Logic explained:

Checked for inequality [if(p[i]!=q[i])] from the first character in both the strings, if find then ASCII difference is returned  (return p[i]-q[i];)

If both the strings reach to the end without inequality then 0 is returned.

 

Program:

#include<stdio.h>

int compare(char*,char*);

void main()

{

   char x[50],y[50];

   clrscr();

   printf("Enter the 1st string:");

   scanf("%s",x);

   printf("Enter the 2nd string:");

   scanf("%s",y);

   if(compare(x,y)==0)

     printf("Equal");

   else if(compare(x,y)>0)

     printf("Biggest string is %s",x);

   else

     printf("Biggest string is %s",y);

   getch();

}

int compare(char *p,char *q)

{

    int i;

    for(i=0;(p[i]!='\0'||q[i]!='\0');i++)

       if(p[i]!=q[i])

              return p[i]-q[i];

    return 0;

}

 

Execution1:

Enter the 1st string: niit

Enter the 2nd string: nice

Biggest string is niit

 

Execution2:

Enter the 1st string: nice

Enter the 2nd string: niit

Biggest string is niit

 

Execution3:

Enter the 1st string: smith

Enter the 2nd string: black

Biggest string is smith

 

Execution4:

Enter the 1st string: niit

Enter the 2nd string: niit

Equal

 

Execution5:

Enter the 1st string: run

Enter the 2nd string: runner

Biggest string is runner

 

strcmp():

In the above example we have developed our own function to compare any two strings.

strcmp() is a predefined function defined within the header file "string.h"

It accepts two strings as arguments and returns the ASCII difference among the first occurrence of unequal characters.

It returns zero if both the strings are equal.

 

Example:

#include<stdio.h>

#include<string.h>

void main()

{

   char x[50],y[50];

   clrscr();

   printf("Enter the first string:");

   scanf("%s",x);

   printf("Enter the second string:");

   scanf("%s",y);

   if(strcmp(x,y)==0)

      printf("Equal");

   else if(strcmp(x,y)>0)

     printf("Biggest string %s",x);

   else

     printf("Biggest string %s",y);

   getch();

}

 

Execution:

Enter the first string: hello

Enter the second string: world

Biggest string world

 

Example explained:

Here we have used predefined function to compare two strings.

 

Specification9:

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>

void main()

{

   char x[50],y[50];

   clrscr();

   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");

   getch();

}

 

Execution1:

Enter any string: liril

Palindrome string

 

Execution2:

Enter any string: madan

Not a palindrome string

 

Specification10:

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.

 


 

 

 

 

Program:

#include<stdio.h>

void main()

{

   char x[50];

   int i,j,flag;

   clrscr();

   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");

   getch();

}

 

Execution1:

Enter any string: madam

Palindrome string

 

Execution2:

Enter any string: madan

Not a palindrome string

 

Specification11:

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

 

Program:

#include<stdio.h>

void main()

{

  

   char name[50],street[50],city[50];

   long int pin;

   clrscr();

   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);

   getch();

}

 

Execution:

Name: subbu

Street: paletivari veedhi

City: Kavali

Pin code: 524201

Address:

Name subbu

Street paletivari veedhi

City Kavali

Pin code 524201

 

Specification12:

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

 

program:

#include<stdio.h>

#include<string.h>

void main()

{

    int m1,m2,m3,tot;

    float avg;

    char result[50];

    clrscr();

    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);

  getch(); 

}

 

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.

 

Program rectified:

#include<stdio.h>

#include<string.h>

void main()

{

    int m1,m2,m3,tot;

    float avg;

    char result[50];

    clrscr();

    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);

  getch();  

}

 

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

 

Example explained:

Here strcpy() is used to assign a string to the array.

 

Accepting a line of text:

The scanf() function is only capable to accept a word but not a line of text.

It considers the space character as a terminating character.

 

Example:

#include<stdio.h>

void main()

{

   char x[100];

   clrscr();

   printf("Enter a line of text:");

   scanf("%s",x);

   printf("The given text is : %s",x);

   getch();

}

 

Execution:

Enter a line of text: hello world

The given text is: hello

 

Example rectified:

#include<stdio.h>

void main()

{

   char x[100];

   clrscr();

   printf("Enter a line of text:");

   gets(x);

   printf("The given text is : %s",x);

   getch();

}

 

Execution:

Enter a line of text: hello world

The given text is: hello world

0 comments:

Post a Comment