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

63.strings :strlen(), strrev(), strcpy()

 

strlen():

In the above example we have developed our own function to find the length.

strlen() is a predefined function defined with in the header file "string.h" used to find the length of string.

It accepts the string (address of array) as argument and returns the length excluding with '\0'.

 

Function definition:

int strlen(char *p)

{

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

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

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

}

 

Example:

#include<stdio.h>

#include<string.h>

void main()

{

   char x[50];

   int l;

   clrscr();

   printf("Enter any string:");

   scanf("%s",x);

   l=strlen(x);

   printf("The length of string %d",l);

   getch();

}

 

 

 

Execution:

Enter any string: America

The length of string 7

 

Example explained:

Here predefined function is used to find the length

 

Specification5:

Accept any string from the keyboard, reverse the string using the function reverse() and print the resultant string.

 

Program:

#include<stdio.h>

void reverse(char*);

void main()

{

   char x[50];

   int i;

   clrscr();

   printf("Enter any string:");

   scanf("%s",x);

   reverse(x);

   printf("The reverse string %s",x);

   printf("%s",x);

   getch();

}

void reverse(char *p)

{

  int i,j;

  char temp;

  for(j=0;p[j]!='\0';j++); /* sends the j to end of string */

  for(i=0,j--;i<j;i++,j--)

  {

     temp=p[i];

     p[i]=p[j];

     p[j]=temp;

  }

}

 

Execution:

Enter any string: india

The reverse string aidni

 

Program explained:

for(j=0;p[j]!='\0';j++);  is terminated with a semicolon sends the "j" to the terminating character '\0'

for(i=0,j--;i<j;i++,j--) loop reverses the string.

 

strrev():

In the above example we have developed a function called reverse() to reverse a string.

strrev() is a predefined function defined within the header file "string.h" used to reverse a string.

It accepts the address of a string as an argument and reverses the string.

 

Function definition:

void strrev(char *p)

{

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

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

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

}

 

Example:

#include<stdio.h>

#include<string.h>

void main()

{

   char x[50];

   int i;

   clrscr();

   printf("Enter any string:");

   scanf("%s",x);

   reverse(x);

   printf("The reverse string %s",x);

   printf("%s",x);

   getch();

}

 

Execution:

Enter any string: india

The reverse string aidni

 

Specification6:

Accept a string onto one character array, copy the string onto another character array using the function copy() and print the resultant array (String copy).

 

Program:

#include<stdio.h>

void copy(char*,char*);

void main()

{

   char x[100],y[100];

   clrscr();

   printf("Enter any string:");

   scanf("%s",x);

   copy(y,x);

   printf("%s",y);

   getch();

}

 

void copy(char *q,char *p)

{

   int i;

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

     q[i]=p[i];

   q[i]='\0';

}

 

Execution:

Enter any string: India

India

 

Example explained:

Because string is a character array we can't copy an array onto another array directly.

By using a loop we need to copy character by character.

At the final a terminating character '\0' is added to the target string.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


strcpy():

In the above example we have developed our own function to copy a string on to another.

strcpy() is a predefined function defined with in the header file "string.h" used to copy a string onto another.

It accepts the addresses of both target and source strings as arguments and copy the contents of Source string on to target string.

 

Function definition:

void strcpy(char *q,char *p)

{

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

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

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

}

 

Example:

#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 onto "y" */

   printf("%s",y);

   getch();

}

 

Execution:

Enter any string: India

India

 

Example explained:

The source string "x" is copied onto "y"

 

Specification: Accept two strings from the keyboard, concatenate a string to another using the function concat() and print the resultant string.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Program:

#include<stdio.h>

void concat(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);

   concat(x,y);

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

   getch();

}

void concat(char *p,char *q)

{

   int i,j;

   for(j=0;p[j]!='\0';j++);   /* setting "j" to the end of string */

   for(i=0;q[i]!='\0';i++,j++) /* copying from source to target */

     p[j]=q[i];

   p[j]='\0';  /* adding terminating character */

 

}

 

Execution:

Enter the 1st string: hello

Enter the 2nd string: india

The resultant string is helloindia

0 comments:

Post a Comment