Thursday, September 20, 2012

program to perform string functions

 it perform string functions
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[20];
int n,t;
clrscr();
printf("1.CONCATINATION\n2.COPYING\n3.COMPARING\n4.STRING LENGTH\n\n");

printf("CHOOSE THE NUMBER \n");
m:
scanf("%d",&n);
switch (n)
{
case 1: printf("\nenter the string 1\n");
       scanf("%s",a);
      printf("\nenter the string 2\n");
       scanf("%s",b);
    strcat( a,b);
    puts(a);
    break;
case 2: printf("\nenter the string \n");
       scanf("%s",b);
    strcpy( a,b);
    printf("The source string is\t%s\n",b);

    printf("The copied string is\t%s",a);

    break;
case 3: printf("\nenter the first string \n");
       scanf("%s",a);
    printf("Enter the second string \n");
       scanf("%s",b);
   t=    strcmp( a,b);
      if(t==0)
      {
      printf("The strings are same\n");
      }
      else
      {
    printf("The strings are NOT same\n");
    }
    break;
case 4: printf("\nenter the string \n");
       scanf("%s",a);
   t=    strlen( a);
    printf("The string length is\t%d",t);

    break;
default: printf("Sorry\t chose any valid option\n");
goto m;
}
getch();
}


 C string functions
#include <string.h>


char *strcpy( char *s1, const char *s2)
  • copies the string s2 into the character array s1.  The value of s1 is returned.
char *strncpy( char *s1, const char *s2, size_t n)
  • copies at most n characters of the string s2 into the character array s1.  The value of s1 is returned.
char *strcat( char *s1, const char *s2)
  • appends the string s2 to the end of character array s1.  The first character from s2 overwrites the '\0' of s1. The value of s1 is returned.
char *strncat( char *s1, const char *s2, size_t n)
  • appends at most n characters of the string s2 to the end of character array s1.   The first character from s2 overwrites the '\0' of s1. The value of s1 is returned.
char *strchr( const char *s,  int c)
  • returns a pointer to the first instance of c in s.  Returns a NULL pointer if c is not encountered in the string.
char *strrchr( const char *s,  int c)
  • returns a pointer to the last instance of c in s.  Returns a NULL pointer if c is not encountered in the string.
int strcmp( const char *s1, const char *s2)
  • compares the string s1 to the string s2.  The function returns 0 if they are the same, a number < 0 if s1 < s2, a number > 0 if s1 > s2.
int strncmp( const char *s1, const char *s2, size_t n)
  • compares up to n characters of the string s1 to the string s2.  The function returns 0 if they are the same, a number < 0 ifs1 < s2, a number > 0 if s1 > s2.
size_t strspn( char *s1, const char *s2)
  • returns the length of the longest substring of s1 that begins at the start of s1and consists only of  the characters found in s2.
size_t strcspn( char *s1, const char *s2)
  • returns the length of the longest substring of s1 that begins at the start of s1and contains none of the characters found in s2.
size_t strlen( const char *s)
  • determines the length of the string s.  Returns the number of characters in the string before the '\0'.
char *strpbrk( const char *s1,  const char *s2)
  • returns a pointer to the first instance in s1 of any character found in s2.  Returns a NULL pointer if no characters from s2 are encountered in s1.
char *strstr( const char *s1,  const char *s2)
  • returns a pointer to the first instance of string s2 in s1.  Returns a NULL pointer if s2 is not encountered in s1.
char *strtok(char *s1, const char *s2)
  • repeated calls to this function modifies string s1 by breaking it into "tokens"--that is the string is broken into substrings, each terminating with a '\0', where the '\0' replaces any characters contained in string s2. The first call uses the string to be tokenized as s1; subsequent calls use NULL as the first argument. A pointer to the beginning of the current token is returned; NULL is returned if there are no more tokens.
    Warning: this changes the original string!

No comments:

Post a Comment