Thursday, 23 February 2012

String functions in C and C++ programming

,
String input\output function


Gets( )
It is used to read a set of alphanumeric character from stdin until a carriage return key is pressed and places a NULL in memory or returns.

Syntax

Gets(variable name);
Program:

#include<stdio.h>
Main()
{
Char  string[5];
Gets(string);
}


Puts( )
Puts( ) function used to send characters   as output that is to stdout from stored data until it run into the NULL when it sends a newline and returns.

Syntax

Puts(variable name);
Program:

#include<stdio.h>
Main()
{
Char string[5];
Gets(string);
Puts(string);
}


sscanf( )
This function is same as scanf function but the difference is that it read data stored in an array instead of  data entered by keyboard.

Syntax:

Sscanf(memory name, “control characters”, variable name);


Program:

#include,stdio.h>
Main()
{
Char string[5];
Int I,j;
Float a,b;
Gets(string);
Sscanf(string, “%d%d%f%f\n”, I, j, a, b);
}


Sprint( )
Like printf this function also use to enter data, but the difference is that sprint  write  formatted data in array istead of memory.

Syntax:

Sprint(memory name, “control character”, variable name);
Program:

#include<stdio.h.
Main()
{
Char string[5];
Int I,j;
Float a, b;
Gets(char);
Sprint(string, “%d%d%f%f\n”, I , j, a, b);
}


String handling functions


Strcmp( )
This function compares two strings character by character and returns -1 if ASCII value of first string is less then second string, 0 if AsCII value of both string is same, and 1 if ASCII  value of first string is greater then second string.
Syntax:

Strcmp(string1, string2);


Strcpy( )
This function is used to copy one string to another.

Syntax:

Strcpy(string1, string2);


Example: strcpy(“mouse”,”keyboard”);
This copies the string keyboard to mouse. Here , the current content of mouse is lost.

Strcat( )
This function is used to concatenate two strings. That is it appends one string at the end of other string.
Syntax:

Strcat(string1, string2);

Example: (“computer”, “concepts”);

Output will be
 computer concepts.

Strlen( )

This returns the number of characters in string.
Syntax: strlen(string);
Example: strlen(“computer”);
Output will be:
8

Strcmp( )
This function compares the first n character of two input strings.
Syntax: (string1, string2, n);
Example: strcmp(“elephant”,” lion”, 4);
It compares starting 4 strings and output will be as that of strcmp()  dissed.

Strncpy( )
This copies first n characters of second string to the first string.
Syntax: strncpy(string1, string2, n);
Example: strncpy(“sachin”, “tendulkar”,6);
Output will be
Tendul
And sachin string will lost.

Strncat( )
This appends the first n character of second string to the end of first string,
Syntax: strncat(string1,string2);
example: strncat(“system”, “software engg”, 8);
output will be:
 system software

strlwr( )
this function converts string into lower case.
Syntax: strlwr(string);
Example: strlwr(“COMPUTER”);
Output will be
computer

strupr( )
it converts string into upper case.
Syntax: strupr(string1);
Example: strupr(“computer”);
Output will be:
COMPUTER

Strchr( ) 
This function search for the character provided in string.it returns NULL if the desired character is not found in the string.
Syntax: strchr(string, desired_char);
Example: strchr(“computer”, t);
Since t is in the string search is successful.

1 comment:

  1. Can u please tell me with an example on how to use strncat.
    thanx

    ReplyDelete