15.2 – Reding and Writing a string

by subbu on August 6, 2014

Simple way of Reading and writing a string

As we discoursed in the previous session, it is confirmed that a string must be stored in a character array but, it is a cumbersome job to read and write a string character by character using a loop every time. C language simplifies the process of reading and writing of string through printf() and scanf().

Reading a string

It is enough to specify the address of character array with its name and a format specifier %s to the scanf() to read and store the string into the character array. The scanf() automatically reads the string character by character, stores into the character array and a terminating character ‘\0’ is automatically added at the end.

char x[50];

scanf("%s",x);

reading a string using scanf()

Specification: Accept a string from the keyboard and print by using a loop.
Program:

#include<stdio.h>
int main()
{
char x[50];
int i;
printf("Enter any string:");
scanf("%s",x);
printf("The given string:");
for(i=0;x[i]!='\0';i++)
printf("%c",x[i]);
return 0;
}

Execution:
Enter any string: rocking
The given string rocking

Program explained:


scanf("%s",x);

Here The scanf() is accepting the string “rocking” from the keyboard, stores in the character array character by character and a terminating character ‘\0’ is automatically added.

for(i=0;x[i]!='\0';i++)
    printf("%c",x[i]);

Here the loop could manage to print the string character by character until ‘\0’. By this it is proved that a string terminator ‘\0’ is automatically added by the scanf()

Printing a string

Similar to scanf(), if we give the address of character array with its name and a format specifier %s to the printf(), the printf() automatically prints the string character by character until the terminating character ‘\0’

printing a string using print()

Why a string is terminated with ‘\0’

The inner story behind printing of string is, when we specify the name of character array in printf() statement, it is the address that is send as argument to the printf() statement. Through the pointer, the printf() function read and prints the characters until string terminator ‘\0’ is get.

It is the reason why the string terminator ‘\0’ is automatically added by the C runtime otherwise printf() could not expect the end of string.

Specification: Accept any string from the keyboard and print the same without using a loop.
Program:

#include<stdio.h>
int main()
{
char x[50];
int i;
printf("Enter any string:");
scanf("%s",x);
printf("The given string:");
printf("%s",x);
return 0;
}

Execution:
Enter any string: rocking
The given string rocking

Program explained:
Here scanf() and printf() are used to accept and print the strings automatically.

Specification: Accept any string from the keyboard and print using the function display()
Program:

#include<stdio.h>
void display(char*);
int main()
{
char x[50];
printf("Enter any string:");
scanf("%s",x);
display(x);
return 0;
}
void display(char *p)
{
printf("%s",p);
}

Execution:
Enter any string: rocking
rocking

Program explained:
sending address of string as argument

Here while sending a string as an argument we specified the name of string (x) as an argument which is the address of array.
A character pointer (p) is defined as a formal argument to store the address of character array.
Now both “x” and “p” refers the same array from different functions.
As “p” holds the address of array, string would be printed when “p” is send as argument to the printf()

Now let us see another version of display() function

void display(char *p)
{
int i;
for(i=0;p[i]!='\0';i++)
printf("%c",p[i]);
}

indirection with string

As “p” is the pointer to character array which holds the string, “p” could access every element of character array with p[i] where i=0,1,2…..until p[i]!=’\0′

Even “p” is capable to access every element of character array using address arithmetic. As the size of char type is 1 byte, p+0 (6232) refers the first element, p+1 (6233) refers the second element and so on.
Now “p” could access every element of character array with indirection *(p+i) where i=0,1,2….until *(p+i)!=’\0′.

So another version of display() function is

void display(char *p)
{
int i;
for(i=0;*(p+i)!='\0';i++)
printf("%c",*(p+i));
}

Previous post:

Next post: