Monday, 9 January 2012

Strings in C

Character type array is called string.All strings end with the NULL character.Use the %s placeholder in the printf() function to display string values.In other words to create a string in C you create an array of chars and set each element in the array to a char value that makes up the string. When sizing the string array you need to add plus one to the actual size of the string to make space for the null terminating character, "\0"
char name[50];

The above statement declares a string called name that can take up to 50 characters. It can be indexed just as a regular array as well.
name[] = {'t','w','o'};
Character  t  w o \0
ASCII Code 116 119 41 0
The last character is the null character having ASCII value zero.
n fact, C’s only truly built-in string-handling is that it allows us to use string constants (also called string literals) in our code. Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character. For example, we can declare and define an array of characters, and initialize it with a string constant:
        char string[] = "Hello, world!";
In this case, we can leave out the dimension of the array, since the compiler can compute it for us based on the size of the initializer (14, including the terminating \0). This is the only case where the compiler sizes a string array for us, however; in other cases, it will be necessary that we decide how big the arrays and other data structures we use to hold strings are.
To do anything else with strings, we must typically call functions. The C library contains a few basic string manipulation functions, and to learn more about strings, we’ll be looking at how these functions might be implemented.
Since C never lets us assign entire arrays,we use the strcpy function to copy one string to another.
Before starting programming with a String functions we should the include the following header file.
  • In C language Strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL '\0' character:
  • It is important to preserve the NULL terminating character as it is how C defines and manages variable length strings. All the C standard library functions require this for successful operation.
  • All the string handling functions are prototyped in: string.h or stdio.h standard header file. So while using any string related function, don't forget to include either stdio.h or string.h. May be your compiler differes so please check before going ahead.
  • If you were to have an array of characters WITHOUT the null character as the last element, you'd have an ordinary character array, rather than a string constant.
  • String constants have double quote marks around them, and can be assigned to char pointers as shown below. Alternatively, you can assign a string constant to a char array - either with no size specified, or you can specify a size, but don't forget to leave a space for the null character!
char *string1 = "Hello";
char string2[] = "Hello";
char string3[6] = "Hello";
Functions in C
  • Strlen()
    size_t strlen ( const char * str );
    This function is used to find the length of the string.
  • strrev()This function is used to reverse the given string.
  • strcpy()
    char * strcpy ( char * destination, const char * source );
    This function is used to copy the string from one array variable to another array variable.
  • strcat()
    char * strcat ( char * destination, const char * source );
    This function is used to concatenation of two strings.  
  • strncpy()
    char * strncpy ( char * destination, const char * source, size_t num );
    This function of used to copy the string from source to destination but only a fixed n bytes is allowed to copy.
  • strncat()
    char * strncat ( char * destination, char * source, size_t num );
    This function of used to con-cat the string from source to destination but only a fixed n bytes is allowed to con-cat.
  • strcmp()
    int strcmp ( const char * str1, const char * str2 );
    Compares the C string str1 to the C string str2.
    This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
  • strcmpi() or stricmp()
    int strcmpi( const char *s1, const char *s2 );
    The strcmpi() function compares, with case insensitivity, the string pointed to by s1 to the string pointed to by s2. All uppercase characters from s1 and s2 are mapped to lowercase for the purposes of doing the comparison. The strcmpi() function is identical to the stricmp() function.
  • strrchr()const char * strrchr ( const char * str, int character );
          char * strrchr (       char * str, int character );
    Locate last occurrence of character in string
    Returns a pointer to the last occurrence of character in the C string str.
    The terminating null-character is considered part of the C string. Therefore, it can also be located to retrieve a pointer to the end of a string.
  • memcmp()
    int memcmp ( const void * ptr1, const void * ptr2, size_t num );
    Compare two blocks of memory
    Compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match or a value different from zero representing which is greater if they do not.

No comments:

Post a Comment

host gator coupon