Miyerkules, Disyembre 7, 2011

String.h

▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓
▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒ 
▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓
▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓ 

String.h
--string.h is the header in the C standard library for the C programming language which contains macro definitions, constants, and declarations of functions and types used not only for string handling but also various memory handling functions; the name is thus something of a misnomer.

String copy functions
•strcpy() - copies a string to another
•strncpy() - copies n characters from a string to another

SAMPLE PROGRAM #1 [strcpy()]


#include <string.h>
  #include <stdio.h>
  #include <conio.h>
  int main()
  {
  char s1[100],s2[100];
  strcpy(s1,"hello");
  printf(“%s”,s1);
  getch();
  }


SAMPLE PROGRAM #2 [strncpy()]


#include <string.h>
  #include <stdio.h>
  #include <conio.h>
  int main()
  {
  char s1[100],s2[100];
  strncpy(s1,"hello“,2);
  printf(“%s”,s1);
  getch();
  }

String concatenation
•strcat() - concatenates two strings
•strncat() - concatenates n characters from a string onto another




SAMPLE PROGRAM #3 [strcat()]

  #include <stdio.h>
  #include <string.h>
  int main ()
  {
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str); //printf in string.h
  getch();
  }


String Compare
•strcmp() - compares two strings
•strncmp() - compares n characters of two strings


SAMPLE PROGRAM #4 [strcmp()]

  #include <stdio.h>
  #include <string.h>
  int main()
  {
  char s[10] = "testing", t[10];
  strcpy(t, s);
   if (strcmp(s, t)==0)
  printf("The strings are identical.\n");    else
  printf("The strings are different.\n");
  getch();
  }


Other String Functions
•strchr() - returns the first occurrence of character in a string
•strrchr() - returns the last occurrence of character in a string
•strspn() - returns the length of leading characters in a string that are contained in a specified string
•strcspn() - returns the length of leading characters in a string not contained in a specified string
 

SAMPLE PROGRAM #5 [strchr()]

  #include <stdio.h>
  #include <string.h>
  int main ()
  {
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n“,str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
  printf ("found at %d\n",pch-str+1);           

  pch=strchr(pch+1,'s');
  }
  getch();
  }


[OUTPUT]
Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18


SAMPLE PROGRAM #6 [strrchr()]


  #include <stdio.h>
  #include <string.h>
  int main ()
  {
  char str[] = "This is a sample string";
  char * pch;
  pch=strrchr(str,'s');
  printf ("Last occurence of 's' found at %d \n",pch-str+1);
  return 0;
  }


[OUTPUT]
Last occurrence of 's' found at 18

CONTINUATION
•strpbrk () - returns first occurrence in a string of any character in another string
•strstr () - returns the first occurrence of one string in another
•strlen()  - returns length of a string
•strtok()  - returns a token from a string delimited by specified characters
•strrev() – reverses all characters except the null terminator.

SAMPLE PROGRAM #7 [strpbrk()]


  #include <stdio.h>
  #include <string.h>
  int main ()
  {
  char str[] = "This is a sample string";
  char key[] = "aeiou";
  char * pch;
  printf ("Vowels in '%s': ",str);
  pch = strpbrk (str, key);
  while (pch != NULL)
  {
  printf ("%c " , *pch);
  pch = strpbrk (pch+1,key);
  }
  printf ("\n");
  return 0;
  }


[OUTPUT]
Vowels in 'This is a sample string': i i a a e i

SAMPLE PROGRAM #8 [strstr ()]


#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  strncpy (pch,"sample",6);
  puts (str);
  return 0;
}


[OUTPUT]
This is a sample string
 

SAMPLE PROGRAM #9 [strlen()]

#include <stdio.h>
#include <string.h>
int main ()
{
  char szInput[256];
  printf ("Enter a sentence: ");
  gets (szInput);
  printf ("The sentence entered is %u characters long.\n",strlen(szInput));
  return 0;
}


[OUTPUT]
Enter sentence: just testing
The sentence entered is 12 characters long.

SAMPLE PROGRAM #10 [strtok()]


#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
  printf ("%s\n",pch);
  pch = strtok (NULL, " ,.-");
   }
  return 0;
}

[OUTPUT]
Splitting string "- This, a sample string." into tokens:
This
a
sample
string

SAMPLE PROGRAM #11 [strtok()]


#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] =“reverse";
  strrev(str);
  puts(str);
  return 0;
}

[OUTPUT]
esrever


▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓
▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒ 
▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓ 
▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒

Holiday picture Collection


                                                                   AC II art