Eastern Mediterranean University School of Computing and Technology Information Technology Lecture7 Strings and Characters Using Strings 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 msg[6]= h, e, l, l, o ; If you follow the rule of array initialization then you can write the above statement as follows: char msg[6]= Hello ; A string of characters is stored in successive elements of a character array and terminated by the NULL character. For example, the string "Hello" is stored in a character array, msg[], as follows: char msg[6]= Hello ; msg[0] = 'H'; msg[1] = 'e'; msg[2] = 'l'; msg[3] = 'l'; msg[4] = 'o'; msg[5] = '\0'; If you want the user to input his or her name, you must use a string. Using scanf() to input a string works, but it will terminate the string after it reads the first space. There are several approaches to handling this problem, but probably the simplest and safest is to use the gets function. Following is the memory presentation of above defined string in C/C++: 1 P a g e
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: char msg[6] = 'H', 'e', 'l', 'l', 'o', '\0'; printf("greeting message: %s\n", msg ); When the above code is compiled and executed, it produces result something as follows: Greeting message: Hello Ex: This program reads the word character by character and then prints out the string char str[20]="hello"; int i=0; while(str[i]!='\0') putchar(str[i]); i++; printf("\n"); Ex: This program reads characters until a newline, stores them in an array and terminates the string with a NULL character. It then prints out the string. 2 P a g e
char str[20],ch; int i=0; printf("enter some characters:\n"); ch=getchar(); while(ch!='\n') str[i]=ch; i++; ch=getchar(); //*(str+i)=ch; str[i]='\0'; // *(str+i)=null; or *(str+i)=0; printf("\nthe string is:\n"); i=0; while(str[i]!='\0') putchar(str[i]); // putchar(*(str+i)); i++; //puts(str); printf("\n"); Strings and Pointers Below is how you might use a character pointer to keep track of a string. char label[] = "Single"; char label2[10] = "Married"; char *labelptr; labelptr = label; We would have something like the following in memory (e.g., supposing that the array label started at memory address 2000, etc.): label @2000 ------------------------------ S i n g l e \0 ------------------------------ label2 @3000 ------------------------------------------ M a r r i e d \0 ------------------------------------------ labelptr @4000 -------- 2000 -------- 3 P a g e
Passing Strings Just as we can pass other kinds of arrays to functions, we can do so with strings. Ex: void func(char *p) int i=0; while(*(p+i)!='\0') putchar(*(p+i)); i++; printf("\n"); char str[25]; printf("enter a message:\n"); gets(str); func(str); C supports a wide range of functions that manipulate null-terminated strings: S.N. Function & Purpose 1 2 3 4 strcpy(s1, s2); Copies string s2 into string s1. strcat(s1, s2); Concatenates string s2 onto the end of string s1. strlen(s1); Returns the length of string s1. strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. Strlen(str) function The C library function strlen(str) computes the length of the string str up to but not including the terminating null character. Parameters str -- This is the string whose length is to be found. 4 P a g e
Example The following example shows the usage of strlen() function. char str[50]; int len; strcpy(str, "This is example"); len = strlen(str); printf("length of %s is %d \n", str, len); Length of This is an example is 26 Strcpy(dest,src) function The C library function strcpy(dest,src) copies the string pointed to by src to dest. Parameters dest -- This is the pointer to the destination array where the content is to be copied. src -- This is the string to be copied. Example The following example shows the usage of strcpy() function. char src[40]; char dest[100]; strcpy(src, "This is example "); strcpy(dest, src); printf("final copied string : %s\n", dest); Final copied string : This is an example 5 P a g e
Strcat(dest,src) function The C library function strcat(dest, src) appends the string pointed to by src to the end of the string pointed to by dest. Parameters dest -- This is pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string. src -- This is the string to be appended. This should not overlap destination. Example The following example shows the usage of strcat() function. char src[50], dest[50]; strcpy(src, "This is source"); strcpy(dest, "This is destination "); strcat(dest, src); printf("final destination string : %s \n", dest); Final destination string : This is destination This is source Strcmp(dest,src) function The C library function strcmp(str1, str2) compares the string pointed to by str1 to the string pointed to by str2. Parameters str1 -- This is the first string to be compared. str2 -- This is the second string to be compared. Return Value This function returned values are as follows: 6 P a g e if Return value if < 0 then it indicates str1 is less than str2 if Return value if > 0 then it indicates str2 is less than str1
Example if Return value if = 0 then it indicates str1 is equal to str2 The following example shows the usage of strncmp() function. char str1[15]; char str2[15]; int ret; strcpy(str1, "abcdef"); strcpy(str2, "ABCDEF"); ret = strcmp(str1, str2); if(ret > 0) printf("str1 is less than str2\n"); else if(ret < 0) printf("str2 is less than str1\n"); else printf("str1 is equal to str2\n"); str1 is less than str2 strrev() function strrev( ) function reverses a given string in C language. Syntax for strrev( ) function is given below. strrev(string); strrev( ) function is non standard function which may not available in standard library in C. 7 P a g e
Example program for strrev() function in C: In below program, string Hello is reversed using strrev( ) function and output is displayed as olleh. char name[30] = "Hello"; printf("string before strrev( ) : %s\n",name); printf("string after strrev( ) : %s\n",strrev(name)); String before strrev(): hello String after strrev(): olleh Following example makes use of few of the above-mentioned functions: 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 ); 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 8 P a g e