0% found this document useful (0 votes)
88 views

Built in Function

The document summarizes 8 built-in functions of "stdio.h" and "string.h" headers in C++. It explains the functions putchar(), gets(), puts() for character input/output. It also explains string functions like strlen() to get string length, strcmp() to compare strings, strcpy() to copy strings, and strncpy() to copy a specified number of characters. Examples are provided for each function.

Uploaded by

ShAmì Náwàȝ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Built in Function

The document summarizes 8 built-in functions of "stdio.h" and "string.h" headers in C++. It explains the functions putchar(), gets(), puts() for character input/output. It also explains string functions like strlen() to get string length, strcmp() to compare strings, strcpy() to copy strings, and strncpy() to copy a specified number of characters. Examples are provided for each function.

Uploaded by

ShAmì Náwàȝ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Built in function of "stdio.h":1)The "putchar" Function:It is stand for Put character.

It is used to put or print a single character on the standard output device i.e
computer screen.

Example:#include<iostream>
using namespace std;
#include "stdio.h"
main()
{ char a;
cout<<"enter any char"<<endl;
cin>>a;
putchar(a);
}

2) The "gets" function:The function is used to get data into a string variable from the keyboard. Any type of
characters, including spaces and special character can be entered using this function.enter key is
pressed after entering the string.when a enter key is pressed a null character is appended at the
end of the string.

Its Syntax Is:


gets(strvar);
Where

Strvar represents the string type varaible into which data is to be entered.
Example:#include<iostream>
using namespace std;
#include "stdio.h"
main()
{ char a[5];
cout<<"enter any name"<<endl;
gets(a);
cout<<a;
}

3) The "puts" function:it is used to print a string on the screen. the new line is inserted after printing the
string.
Its Syntax is:

puts(string);
The string may be string variable or string type varaiable.
in case of string constant it is written in double quotes. Otherwise it is written without using
quotes.

Example:#include<iostream>
using namespace std;
#include "stdio.h"
main()
{ char a[5];

cout<<"enter any name"<<endl;


gets(a);
puts(a);
}

Built in Function of "string.h"


4) The "strlen" Function:The strlen stands for string length.
This function is used to find the length of a string. it counts the totalnumber of character
including spaces . Null character is excluded.

it syntax is :
int strlen (a);
a represents the string whose number of character are to be counted. it will return integer
type value.

Example:#include<iostream>
using namespace std;
#include "string.h"
#include <stdio.h>
#include <string.h>
main()
{
char s[256];
cout<<"Enter a sentence: ";

gets (s);
printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(s));
return 0;
}

5) the "strcmp" function:The strcmp function is used to compare two string.it compare first string to second string
character by character.

It syntax Is:
Int strcmp (string1,string2);
It will return integer type value.

Example:#include<iostream>
using namespace std;
#include "string.h"
#include <stdio.h>
main ()
{
char key[] = "apple";
char buffer[80];
do
{
cout<<"Guess my favorite fruit? ";
fflush (stdout);
scanf ("%79s",buffer);
}
while (strcmp (key,buffer) != 0);
cout<<"Correct answer!";
return 0;
}

6) The "strncmp" function:this function will compare only specified number of characters of the two strings.

it syntax is:
int strncmp (string1,string2,n)
Example:#include<iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
main ()
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
cout<<"Looking for R2 astromech droids...";
for (n=0 ; n<3 ; n++)
if (strncmp (str[n],"R2xx",2) == 0)
{
printf ("found %s\n",str[n]);
}
}

7) The "strcpy" function:it is used to copy the contents of one string to another string including null character.

its syntax is:


strcpy (string1,string2);

Example:#include<iostream>
using namespace std;
#include <stdio.h>

#include <string.h>
main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;
return 0;
}

8) The "strncpy" function:it is used to copy special contents of one string to another.
its syntax is :
strncpy(string1,string2,n);

Example:#include<iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
main ()
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
strncpy ( str2, str1,8 );
strncpy ( str3, str2, 5 );
str3[5] = '\0';
puts (str1);

puts (str2);
puts (str3);
}

You might also like