Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

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.

Saturday, 18 February 2012

Strings in C++, Strings theory and concepts in C++


String

: A string is a sequence of characte that is treated as a single data item.

->

Functions used in string:-


   1. scanf() : It is a input function can be used with "%s" format specification to read in a string of character.
                   Example:
                             char address[10]
                             scanf("%s", address);
   2. getchar(): Use of this function to read single character from the input and place them in to character array.
                   Example:
                             char ch;
                             ch = getchar();
      'getchar' function has no parameters.
 
   3. gets() : It is a library function available in the <stdio.h> header file2  is used to read character in to string from the keyboard.
            This is a simple function wiyh one  string parameter.
                         
                              gets(str)
                   Example:
                             char line [80];
                             gets (line);
                             printf("%s", line);
 
   4. printf() : Used this function with "%s" format to print string to the screen.
                 
                   Example:
                             printf("%s", name);

   5. putchar() : This function is ysed to write character to the screen. And can use this function repeatedly to output a string
                  of character stored in an array using a loop.
                   ->Function putchar requires one parameter.
                   Example:
                             char name[6] = "PARIS";
                             for (i=0; i<5; i++)
                                  putchar(name[i]);
 
   6. puts() : This function is used for printing string values is to use the function 'puts' declared in the header file
               <stdio.h>.
               This is a one parameter function.
                             puts(str);
 
                                           

String-Handling Functions:


   1. strcat() : This functon join two string together. It takes the following form:
                             strcat(string1 , string2);

   2. strcmp()  : The strcmp function compares two strings identified by the arguments and hae the value 0 if they
                  are equal.
                             strcmp(string1 , string2);

   3. strcpy() : The strcpy function works almost like a string-assignment operator i.e is used copy second string in to
                 first string. It take forms :
                           
                             strcpy(string1 , string2);

   4. strlen() : The strlen function is used for count(count length of string) and returns the number of character in a
                 string. It takes form:
                         
                             n = strlen(string);
                         where n is a integer variable , which recieve the value of the length of the string.
                 The counting ends at the first null chartacter.

Other String Function:


   1. strncpy : This function copies only the left-most n characters of the surce string to the terget string variable.
                This is the three-parameter function and is invoked as follows:
                             strncpy(s1, s2, 5);
 
   2. strncmp : A variation of the function strcmp is the strncmp. This function has three-parameters as like:
                             strncmp(s1, s2, n);
                this compares the left-most n character of s1 to s2 and returns.
                (a) 0 if they are equal;
                (b) negative number, if s1 substring is less then s2;
                (c) positive number, otherwise.

   3. strncat : This is another concatenation function tht takes three parameter as shown below:
                       
                             strncat(s1, s2, n);
                this call will concatenate the left-most n character of s2 to the end of s1.

   4. strstr :  It is a two parameter function that can be used to locate sub-string in a string. This takes the forms:
         
                             strstr (s1, s2);            
                             strstr (s1, "ABC");