---Advertisement---
Learn C

Learn C Language : String

By Smart Answer

Updated on:

---Advertisement---

Table of Contents

What Is String

The way a group of integer can be stored in an integer array, similarly a group of character can be stored in a character array.Character array many a times called as strings.Most languages (Basic, Fortran etc.) internally treat strings as character array, but somehow conceal this fact from programmer.Character arrays or strings are used by programming langauges to manipulate text such as words and sentences.

A string constant is a one dimensional array of characters terminated by a null character (‘�’).

For Example,

char name[ ] = {‘H’ ,’A’ ,’E’ ,’S’ ,’L’ ,’E’ ,’R’ ,’�’};

 

Each character in the array occupies 1 byte of memory and the last character is always ‘�’.
What character is this?
It looks like two characters, but it is actually only one character, with the indicating that what follow it is something special.’�’ is called null character.Note that ‘�’ and ‘0’ are not same.ASCII value of ‘�’ is 0, whereas ASCII value of 0 is 48.
Note that the elements of the character array are stored in contiguous memory locations.

The terminating null (‘�’) 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 ‘�’ is not really a string, but merely collection of characters.

Another way of initializing string is:

char name[ ] = “HAESLER”;

 

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

Standard Library String Functions

strlen()

A simple program which demonstrate strlen():

/* Function counts the number of character present in a string */

main()
{
char arr[ ] = “Bamboozied”;
int len1, len2;

      len1 = strlen(arr);
len2 = strlen(“Humpty Dumpty”);

printf(“string = %s length = %d”,arr,len1);
printf(“string = %s length = %d”,”Humpty Dumpty”,len2);
}

Output
string = Banboozied length = 10
string = Humpty Dumpty length = 13

 

Note that in the first call the function strlen(), we are passing the base address of the string, and the function in turn returns the length of the string.While calculating the length it doesn’t count ‘�’, Even in the second call, what gets passed to strlen() is the address of the string and not the string itself.

strcpy()

This function copies the content of one string into another.the base addresses of the source and target string should be supplied to this function.

A simple program which demonstrate strlen():

/* Function copy one string into another */

main()
{
char source[ ] = “Sayonara”;
char target[20];

      strcpy(target, source);

printf(“source string = %s”,source);
printf(“target string = %s”,target);
}

Output
source string = Sayonara
target string = Sayonara

 

On supplying the base addresses, strcpy() goes on copying the characters in source string into target string till it doesn’t encounter the end of the source string (‘�’).It is our responsibility to see to it that the target string dimension is big enough to hold the string being copied into it.Thus, a string gets copied into another, piecemeal, character by character.There is no short-cut fr this.

strcmp()

This is a function which compare two string 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() return value zero.If they are not, it return the numeric difference between the ASCII value of the non-matching characters.

A simple program which demonstrate strcmp():

/* Function compare two strings */

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 “,i,j,k);
}

Output
0 4 -32

 

strcat()

This function concatenates the source string at the end of the target string.For example, “Bombay” and “Nagpur” on cancatenation would result into a string “BombayNagpur”.

A simple program which demonstrate strcat():

/* Function concatenate one string after other */

main()
{
char source[ ] = “Folks”;
char target[20] = “Hello”;

      strcat(target, source);

printf(“source string = %s”,source);
printf(“target string = %s”,target);
}

Output
source string = Folks
target string = HelloFolks

 

Limitations

When we are using an array of pointer to strings we can initialise the strings at the place where we are declaring the array, but we cannot receive the strings from keyboard using scanf().

Thus, the following program would never work out.

main()
{
char *names[6];
int i;
for(i=0; 5>=i; i++)
{
printf(“Enter name”);
scanf(“%s”,&name[i]);
}
}

 

The program doesn’t work because, when we are declaring the array it is containing garbage value.And it would be definitely wrong to send these garbage values to scanf() as the addresses where it should keep the strings received from the keyboard.

Smart Answer

---Advertisement---

Related Post

Learn C Language : Array

‹ previous Next › What are Arrays For understanding the arrays properly, consider the following program: main(){     int x;      x = 5;      x ...

Learn C Language : Data Type

‹ previous Next › Integer, long and short Sometimes, we know in advance that the value stored in a given integer variable will always be positive.When it is ...

Learn C Language : Functions

‹ previous Next › What is a Function A function is a self-contained block of statements that perform a coherent task of some kind.Every c program can be ...

Learn C Language : Switch Case Statement (Case Control Structure)

‹ previous Next › Switch Case Statement The control statement which allows us to make a decision from the number of choices is called a switch, or more ...

Leave a Comment