• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
String & its application
 

String & its application

on

  • 845 views

 

Statistics

Views

Total Views
845
Views on SlideShare
845
Embed Views
0

Actions

Likes
0
Downloads
14
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    String & its application String & its application Presentation Transcript

    • STRINGS AND ITSAPPLICATIONS
    • CONTENTS Introduction to Strings. Two Ways of using Strings. Initializing a string. Print a string. String Functions and its applications.
    • Introduction to Strings  A String is an array of characters.  Strings are stored in an array of char type along with the null terminating character "0" at the end.  When sizing the string array we need to add plus one to the actual size of the string to make space for the null terminating character, "0"  But the null character is not included in the String.  Character Strings are often used to build meaningful and Readable programs. The Common Operations performed on Character Strings include  Reading and Writing Strings.  Combined Strings together.  Copying one String to another.  Comparing Strings for Equality.  Extracting a portion of a String.
    • Two ways of using Strings:  Using with a Character Array  Using with a String Pointer With a Character Array:  A character array is declared in the same way as a normal array. char ca[10];  We must set the value of each individual element of the array to the character We want and we must make the last character as 0. Remember to use %s when printing the string.
    • With String Pointers  String pointers are declared as a pointer to a char. char *sp;  When we assign a value to the string pointer it will automatically put the 0 . char *sp; sp=“Hello”; printf("%s",sp);  We can read a string into only a character array using scanf and not a string pointer.  If we want to read into a string pointer then we must make it point to a character array. char ca[10],*sp; scanf("%s",ca); sp = ca; scanf("%s",sp);
    • Syntax for declaring a String: Syntax to declare a string in C: char string_name[size];  Sample Code: char fname[4];  The above statement declares a string called fname that can take up to 3 characters. It can be indexed just as a regular array as well. fname[] = {t,w,o};  The last character is the null character having ASCII value zero. Initializing a String • To initialize our fname string store the name Ravi, char fname[31] = {“Ravi"}; • We can observe from the above statement that initializing a string is same as with any array. However we also need to surround the string with quotes.
    • Print a String : To write strings to the terminal, we use a file stream known as stdout. The most common function to use for writing to stdout in C is the printf function, defined as follows: int printf(const char *format, ...); To print out a prompt for the user we can: printf("Please type a name: n");  The above statement prints the prompt in the quotes and moves the cursor to the next line.
    • FUNCTIONS INSTRING & ITSAPPLICATION
    • MEMCPY - copy characters to non-overlapping string.• "memcpy" copies exactly N characters from the string "s2" into the area of memory pointed to by "s1“.• Unlike the function "strncpy", "memcpy" does not check for the terminating 0 of string "s2"; it simply copies N characters.• It does not put a terminating 0 on the end of string "s1".Example for memcpy():ptr = memcpy( s1, s2, N );void *s1; points to an area of memory that is to receive the copiedcharacters. This must be able to hold at least N characters.const void *s2; points to the string from which characters will be copied.size_t N; gives the number of characters to copy.void *ptr; points to the copied string (i.e. "s1").
    • MEMMOVE - copy characters to (possibly overlapping) string. • "memmove" moves exactly N characters from the string "s2" into the area of memory pointed to by "s1“. • Unlike the function "strncpy", "memmove" does not check for the terminating 0 of string "s2"; it simply moves N characters. • It does not put a terminating 0 on the end of string "s1". EXAMPLE FOR MEMMOVE(): ptr = memmove( s1, s2, N ); void *s1; points to an area of memory that is to receive the moved characters. This must be able to hold at least N characters. const void *s2; points to the string from which characters will be copied. size_t N; gives the number of characters to copy. void *ptr; points to the copied string (i.e. "s1").
    • STRCPY - copy one string to another.• "strcpy" copies the string "s2" into the area pointed to by "s1“.• This area must be big enough to hold the contents of "s2“.• Since "strcpy" moves the 0 at the end of "s2", the new string "s1" will have the usual terminating 0.EXAMPLE FOR STRCPY():ptr = strcpy( s1, s2 );char *s1; points to an area of memory that is to receive the copiedcharacters.const char *s2; points to the string from which characters will be copied.This must end with the usual 0‘.char *ptr; points to the copied string (i.e. "s1").
    • STRNCPY - copy characters from string. • "strncpy" copies at most N characters from the string "s2" into the area of memory pointed to by "s1“. • If it encounters a 0 before it has copied N characters, it pads "s1" to N characters by adding 0 characters. • If "s2" is more than N-1 characters long, the first N characters will be copied and the string "s1" will NOT receive the usual terminating 0. EXAMPLE FOR STRNCPY(): ptr = strncpy( s1, s2, N ); char *s1; points to an area of memory that is to receive the copied characters. This must be able to hold at least N characters. const char *s2; points to the string from which characters will be copied. size_t N; gives the number of characters to copy. char *ptr; points to the copied string (i.e. "s1").
    • STRCAT - append a string to another.• "strcat" appends a copy of the string "s2" to the end of the string "s1“.• Both strings "s1" and "s2" must be terminated by the usual 0 character.EXAMPLE FOR STRCAT():ptr = strcat( s1, s2 );char *s1; points to a string terminated by the usual 0‘.const char *s2; points to a string that will be appended to "s1“.char *ptr; points to the new string ("s1").
    • STRNCAT - append part of a string to another.• "strncat" appends at most N characters from the string "s2" to the end of the string "s1“.• If string "s2" contains less than N characters, "strncat" will stop when it encounters the terminating 0.EXAMPLE FOR STRNCAT():ptr = strncat( s1, s2, N );char *s1;points to a string terminated by the usual 0‘.const char *s2;points to a string whose characters will be appended to theend of "s1“size_t N;is the maximum number of characters from string "s2" that shouldbe appended to "s1“.char *ptr;points to the new string ("s1").
    • MEMCMP - compare parts of two strings. • "memcmp" compares the first N characters of the string "s1" to the first N characters of the string "s2". • Unlike the function "strncmp", "memcmp" does not check for a 0 terminating either string. Thus it examines a full N characters, even if the strings are not actually that long. EXAMPLE FOR MEMCMP(): i = memcmp( s1, s2, N ); const void *s1, *s2; are the strings to be compared. size_t N; gives the number of characters to be examined. int i; gives the results of the comparison. "i" is zero if the first N characters of the strings are identical. "i" is positive if string "s1" is greater than string "s2", and is negative if string "s2" is greater than string "s1". Comparisons of "greater than" and "less than" are made according to the ASCII collating sequence.
    • STRCMP - compare two strings.• "strcmp" compares the string "s1" to the string "s2". Both strings must be terminated by the usual 0 character.EXAMPLE FOR STRCMP():i = strcmp( s1, s2 );const char *s1, *s2; are the strings to be compared.int i; gives the results of the comparison. "i" is zero if the strings are identical."i" is positive if string "s1" is greater than string "s2", and is negative if string"s2" is greater than string "s1". Comparisons of "greater than" and "less than"are made according to the ASCII collating sequence.
    • STRNCMP - compare parts of two strings.• "strncmp" compares the first N characters of the string "s1" to the first N characters of the string "s2“.• If one or both of the strings is shorter than N characters (i.e. if "strncmp" encounters a 0), comparisons will stop at that point.• Thus N represents the maximum number of characters to be examined, not the exact number.EXAMPLE FOR STRNCMP():i = strncmp( s1, s2, N );const char *s1, *s2; are the strings to be compared.size_t N; gives the number of characters to be examined.int i; gives the results of the comparison. "i" is zero if the first N characters ofthe strings are identical. "i" is positive if string "s1" is greater than string "s2",and is negative if string "s2" is greater than string "s1". Comparisons of"greater than" and "less than" are made according to the ASCII collatingsequence.
    • MEMCHR - find first occurrence of a character in a string.• "memchr" returns a pointer to the first occurrence of the character "c" in the first N characters of the string "s".• Unlike "strchr", "memchr" ignores any 0 bytes it comes across; thus it does not stop if it finds the end of the string, but keeps on going until it has looked at N characters.EXAMPLE FOR MEMCHR():ptr = memchr( s, c, N );const void *s; points to the string to be scanned.int c; is the character to look for.size_t N; is the number of characters to look at before giving up.void *ptr; points to the first occurrence of the character in the string. If thecharacter is not found, the NULL pointer is returned.
    • STRCHR - find first occurrence of a character in a string.• "strchr" returns a pointer to the first occurrence of the character "c" in the string "s".• The 0 that terminates the string is considered part of the string; thus it is possible to find the end of the string with a call like ptr = strchr( s, 0 );EXAMPLE FOR STRCHR():ptr = strchr( s, c );const char *s; points to the string that is to be scanned for the presence ofthe character.int c; is the character to look for.char *ptr; points to the first occurrence of the character in the string. If thecharacter does not appear in the string, the NULL pointer is returned.
    • STRCSPN - scan string for one or more characters. • "strcspn" scans through "s" and finds the first character in "s" that also appears in "stop“.] • It returns the position of that character within "s“. • Another way of saying this is that "strcspn" returns the number of characters at the beginning of "s" that are NOT found in "stop". EXAMPLE FOR STRCSPN(): i = strcspn( s, stop ); const char *s; points to the string to be scanned. const char *stop; points to a string containing the set of "stopping" Characters. size_t i; gives the position of the first character from "stop“ that appears in "s".
    • STRPBRK - find first character from set in string. • "strpbrk" looks through "string" character by character until it finds one of the characters in "set“. • For example, if "set" is a string consisting of a blank and a tab, "strpbrk" would return a pointer to the first blank or tab appearing in "string". EXAMPLE FOR STRPBRK(): ptr = strpbrk(string,set); const char *string; is the string you want to examine. const char *set; gives a set of characters to look for. char *ptr; points to the first character in "string" that also appears in "set". If no characters from "set" can be found in "string", "strpbrk" returns a null pointer.
    • STRRCHR - find last occurrence of a character in a string.• "strrchr" returns a pointer to the last occurrence of the character "c" in the string "s".EXAMPLE FOR STRRCHR():ptr = strrchr( s, c );const char *s; points to the string that is to be scanned for the presence ofthe character.int c; is the character to look for.char *ptr; points to the last occurrence of the character in the string. If thecharacter does not appear in the string, a null pointer is returned.
    • STRSPN - scan string for span of characters.• "strspn" returns the number of characters from the string "set" that appear at the beginning of the string pointed to by "s".EXAMPLE FOR STRSPN().i = strspn( s, set );const char *s; points to the string to be scanned.const char *set; points to a string containing the set of characters to scan for.size_t i; is the number of characters at the beginning of "s" which also appearin the string "set".
    • STRSTR - obtain first substring of string. • "strstr" returns a pointer to the first occurrence of a substring within another string. EXAMPLE FOR STRSTR(): ptr = strstr( s, subs ); const char *s; points to the string to be scanned. const char *subs; points to the (sub)string to scan for. char *ptr; points to the first occurrence of the substring in the given string. If the substring is not found, this will be a null pointer.
    • MEMSET - set memory to specific value. • "memset" sets N bytes to the given value, beginning at the byte pointed to by "p". EXAMPLE FOR MEMSET(): ret = memset( p, value, N ); Where: void *p; points to the beginning of the memory that should be set to the given value. int value; is the value that is to be assigned to each byte. size_t N; is the number of bytes whose value is to be set. void *ret; points to the beginning of the initialized memory.
    • STRERROR - error message associated with number.• The "strerror" function returns a string describing the error associated with the given error number.EXAMPLE FOR STRERROR():msg = strerror(errnum); Where:int errnum; is an error number (that is, one of the recognized values that"errno" may take).char *msg; is a message explaining the meaning of the given error number.
    • STRLEN - find the length of a string. • "strlen" returns the number of characters in a string. EXAMPLE FOR STRLEN(): i = strlen( s ); const char *s; points to the string whose length is to be determined. This string must end with the usual 0‘. size_t i; is the number of characters in the string "s" (not counting the 0).
    • THANK YOU