String in c

String in c

in the previous sessions we learn about float,int,char etc data types, in this session we learn about string. There is no any separate data type for the string, The string is used or string is treat as Array of type char. A character array is string if it ends with the null character(‘\0’). This null character is the escape sequence with the corresponding. ASCII value 0. And it is different from the ASCII character ‘o’. so The string are used to stored or manipulate(change) data in text form like words or sentences.

String constant or String literal

There are some example of string constants are

“N”
“NITISH SARASWAT”
“8676337”
“gandhi ji was the great leader”
“” (null string only contains ‘\0’)

The string constant store some where in memory as an Array of characters and terminated or end by a null character(‘\0’). The string constant also itself a pointer of the first character of the Array.

Lets us take example to understand for example RED FORT will be stored in memory as—

String Memory representing

in the above example each character take one byte and at the end the compiler automatically place null character. in the above example the string There is no any separate data type for the string,“RED FORT” actually the first character is the pointer of this string so here R character is the pointer. so whenever a string constant is used in the program it is replace by pointer point to the string

if we have any pointer variable of char * type, we can assign the address of this string constant to pointer as

char *ptr=”RED FORT”;

ok let us see to clear concept of string see what the expression “Engineering”[6] represent. let us understand according to the equivalence of pointer, This expression is equivalence to *(“Engineering”+6), as we know that Engineering represent the address of first character ‘E’ then on adding 4 to this address we get the address of character ‘e’ and on perform dereferencing operation we will get the character ‘e’.

There is situation where exception is occurs, When the string constant is used for initializing for a character array then this does not represent any address and also it can not store any where in memory. take the example to understand this

char arr[]=”Cisbestprogramming”;

in this example the string “Cisbestprogramming” is not stored any where in the memory or complete string “Cisbestprogramming” is not stored anywhere in memory but this declaration statement reserve space in memory for array arr and is equivalent to-

char arr[]={‘ C ‘ , ‘ i ‘ , ‘ s ‘ , ‘ b ‘ , ‘ e’ , ‘ s ‘ , ‘ t ‘ , ‘ p ‘ , ‘ r ‘ , ‘ o ‘ , ‘ g ‘ , ‘ r ‘ , ‘ a ‘ , ‘ m ‘ , ‘ m ‘ , ‘ i ‘ , ‘ n ‘ , ‘ g ‘};

as in the previous if we have statement like this

char *ptr=”Cisbestprogramming”;

in this example allocate the pointer variable ptr, The string constant is seprately stored in the memory and initialize pointer variable ptr by starting address of “Cisbestprogramming”.

we should understand here some important things such as note that ‘C’ and “C” are different. ‘C’ is character constant which represent the ASCII value of the character ‘C’ but while “C” is the string constant which represent of character C and null character ‘\0’.

String variable

A string variable is a 1-D array of characters terminated by a null character. when we want to declare string variable we need to declare a character array size to hold all the characters of the string including null character

take the example to understand

char str[]={‘ U ‘,’ t ‘,’ t ‘,’ a ‘,’ r ‘,’ p ‘,’ r ‘,’ a ‘,’ d ‘,’ e ‘,’ s ‘,’ h ‘,’\0’};

we can also initialization this as

char str[]=”Uttarpradesh”;

This initialization is same as the previous one but in this case the compiler automatic add null character at the end. but note here this string constant does not represent any address. This array is represent in memory as

character array

In the above example we not fixed or not specify the size of array so this array can hold large enough elements including null character

/* program to display all characters of character array and print the address of every character */
#include<stdio.h>
void
main()
{
char str[]="LoveIndia";
int i;
for(i=0;str[i]!='\0';i++)
{
printf("character=%c \n",str[i]);
printf("Address=%p\n",&str[i]);
}
return 0;
}

/* program to print the character and Address of string using pointer */
#include<stdio.h>
void
main()
{
char *ptr[]="Loveindia";
char *p;
p=ptr;
while(*p!='\0')
{
printf("character=%c\t",*p);
printf("Address=%p\n",p);
p++;
}
return 0;
}

The output of this program is similar as that of previous program let us understand the concept of above example here p is pointer variable which hold the the base address of array ptr[]. increment this pointer every time by 1 so give the next address of array ptr[] every time. here the loop will terminated when the character ‘\0’ encountered which indicates the end of string

we can control string using %s specification also or we can enter and print the string using %s specification with the control string of printf() and scanf()


/* program to input and output string using scanf() and printf() */
#include< stdio.h >
int
main()
{
char fname[20];
printf(“Enter the first name\n”);
scanf(“%s”,&fname);
printf(“\n %s %s”,fname,”Saraswat”);
return 0;
}

1st run Enter the first name
Nitish
Nitish Saraswat

2nd run
Enter the first name
Gaurav sharma
gaurav Saraswat

In the above example the printf() takes the base Address of string and continues display the characters untill the ‘\0’ not encounter. when we enter the string using the %s specification compiler automatic add the ‘\0’ or null character at the end of array. we also don’t need to use & sign in the scanf because the name of the array is address of the array. but in the second run when enter a string with the space we could not get correct result we could get only first string, not get string after space because of scaf() stop reading as soon as it encounters a whitespace.

to get correct result or for entering string with the whitespaces we can use function gets(), it is stop reading the string when it get new line or encounters new line and replace this newline with the null character There is another function puts() which display the string and replaces the null character by a newline

/* Input and output the string using the gets() and puts() */
#include<stdio.h>
void
main()
{
char name[25];
printf("Enter the name :");
gets(name);
printf("The name is :");
puts(name);
return 0;
}