String in C

C - String

String in C : The way a group of integer can be stored in an integer array, similarly a group of characters can be stored in a character array. Character arrays are many a time also called strings. Many languages internally treat strings as character arrays, but somehow conceal this fact from the programmer. Character arrays or strings are used by programming languages to manipulate text such as words and sentences.

A string constant is a one-dimensional array of character terminated by a null (‘\0’).

Example:

char name []={“A”, “S”, “K”, “A”, “T”, “U”, “L”,’
char name []={“A”, “S”, “K”, “A”, “T”, “U”, “L”,’\0’ };
’ };

Each character in the array occupies one byte of memory and the last character is always ‘\0’.It looks like two character, but its is actually only one character , with the \indicating that what follows it is something special. ‘\0’ is called null character. Note that’\0’ and ‘0’ are not same. ASCII value of ‘\0’ is 0, whereas ASCII value of ’0’ is 48.

The terminating null(‘\0’) is important, because it is the only way the functions that work with a string can know where the string ends. In fact, a string not terminated by a ‘\0’ is not really a string, nut merely a collection of charater.

C concedes the fact that you would use strings very often and hence provides a shortcut for initializing strings.

Example:

char name[] = “ASKATUL”;

Note that, in this declaration ‘\0’ is not necessary. C inserts the null character automatically.

More about String

# include<stdio.h>
int main()
{
char name[]= “AskAtul”;
int i=0;

while(i<=7)
{
printf(“%c”, name[i]);
i++;
}
printf(“\n”);
return 0;

}

Output:

AskAtul

Pointers and Strings

Suppose we wish to store “Hello”. We may either store it in a string or we may ask the C compiler to store it at some location in memory and assign the address of the address of the string in a char pointer. This is shown below:

char str[]= “Hello”;
char *p= “Hello”;

There is a subtle difference in usage of these two forms. For example: we cannot assign a string to another, whereas, we can assign a char pointer to the another char pointer. This is shown in the following program.

int main()
{
char str1[]= “Hello”;
char str2[10];
char *s= “Good Morning”;
char *q;
str2 = str1;
q = s;
return 0;
}

Standard Library String Functions

With every C compiler, a large set of useful string handling library functions are provided. Here a list of commonly used functions along with their purpose.

Out of the above list, we shall discuss the functions strlen( ), strcpy( ), strcat( ), and strcmp( ), since they are most commonly used functions.

Strlen( ) : This function counts the number of character present in a string.

Example:

# include <stdio.h>
# include <string.h>
int main()
{
char arr[ ]= “AskAtul”;
int len1, len2 ;

len1 =strlen (arr);
len2 =strlen (“hi learners”);
printf (“string =%slength = %d\n”, arr, len1);
printf (“string = %s length = %d\n”, “hi learners”, len2);
return 0;
}

Output:

string  = AskAtul length =7
string = hi learners length = 11

 strcpy( ): This function copies the contents of one string into another. The base address of the source and target strings should be supplied to this function.

Example:

# include <stdio.h>
# include <string.h>
int main()
{
char source[] = “AskAtul”;
char target[20];
strcpy (target, source);
printf(“source string =%s\n”, source);
printf(“target string =%s\n”, target);
return 0;
}

Output:

Source string = AskAtul
target string = AskAtul

Strcat( ): This function concatenates the source string at the end of the target string.

Example:

# include <stdio.h>
# include <string.h>
int main( )
{
char source[ ]= “Ask”;
char target[30] = “Atul”;
strcat(target source);
printf(“source string =%s\n”, source);
printf (“target string =%s\n”, target);
return 0;
}

Output:

source string = Ask
target string = AskAtul

Strcmp( ): This is a function which compares two strings to find out whether they are same or different. The two strings are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first. If the two strings are identical, strcmp( ) returns a value zero.

Example:

# include <stdio.h>
# include <string.h>
int main( )
{
char string1[ ] = “Jerry”;
char string2[ ] = “Ferry”;
int i, j, k;
i = strcmp (string1,”Jerry”);
j= strcmp (string1, string2);
k= strcmp (string1, “Jerry boy”);
printf(“%d%d%d\n”,I,j,k);
return 0;
}

Recommended Posts

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *