The string in C programming language is actually a one-dimensional array of characters which is terminated by a
null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a
null.
The following declaration and initialization create a string
consisting of the word "Hello". To hold the null character at the end of
the array, the size of the character array containing the string is one
more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above statement as follows:
char greeting[] = "Hello";
Following is the memory presentation of above defined string in C/C++:

Actually, you do not place the
null character at the end of a
string constant. The C compiler automatically places the '\0' at the end
of the string when it initializes the array. Let us try to print above
mentioned string:
#include <stdio.h>
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Greeting message: %s\n", greeting );
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
Greeting message: Hello
C supports a wide range of functions that manipulate null-terminated strings:
| S.N. |
Function & Purpose |
| 1 |
strcpy(s1, s2);
Copies string s2 into string s1. |
| 2 |
strcat(s1, s2);
Concatenates string s2 onto the end of string s1. |
| 3 |
strlen(s1);
Returns the length of string s1. |
| 4 |
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. |
| 5 |
strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1. |
| 6 |
strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1. |
Following example makes use of few of the above-mentioned functions:
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
You can find a complete list of c string related functions in C Standard Library.
- 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 *string_1 = "Hello";
char string_2[] = "Hello";
char string_3[6] = "Hello";
|
Reading and Writing Strings:
One possible way to read in a string is by using
scanf.
However, the problem with this, is that if you were to enter a string
which contains one or more spaces, scanf would finish reading when it
reaches a space, or if return is pressed. As a result, the string would
get cut off. So we could use the
gets function
A
gets takes just one argument - a char pointer, or the name
of a char array, but don't forget to declare the array / pointer
variable first! What's more, is that it automatically prints out a
newline character, making the output a little neater.
A
puts function is similar to
gets function in the
way that it takes one argument - a char pointer. This also automatically
adds a newline character after printing out the string. Sometimes this
can be a disadvantage, so
printf could be used instead.
#include <stdio.h>
int main() {
char array1[50];
char *array2;
printf("Now enter another string less than 50");
printf(" characters with spaces: \n");
gets(array1);
printf("\nYou entered: ");
puts(array1);
printf("\nTry entering a string less than 50");
printf(" characters, with spaces: \n");
scanf("%s", array2);
printf("\nYou entered: %s\n", array2);
return 0;
}
|
This will produce following result:
Now enter another string less than 50 characters with spaces:
hello world
You entered: hello world
Try entering a string less than 50 characters, with spaces:
hello world
You entered: hello
|
String Manipulation Functions
- char *strcpy (char *dest, char *src) - Copy src string string into dest string.
- char *strncpy(char *string1, char *string2, int n) - Copy first n characters of string2 to stringl .
- int strcmp(char *string1, char *string2) - Compare string1 and string2 to determine alphabetic order.
- int strncmp(char *string1, char *string2, int n) - Compare first n characters of two strings.
- int strlen(char *string) - Determine the length of a string.
- char *strcat(char *dest, const char *src); - Concatenate string src to the string dest.
- char *strncat(char *dest, const char *src, int n); - Concatenate n chracters from string src to the string dest.
- char *strchr(char *string, int c) - Find first occurrence of character c in string.
- char *strrchr(char *string, int c) - Find last occurrence of character c in string.
- char *strstr(char *string2, char string*1) - Find first occurrence of string string1 in string2.
- char *strtok(char *s, const char *delim) - Parse the string s into tokens using delim as delimiter.
This comment has been removed by the author.
ReplyDelete