Accessing strings C

The following program shows how data is entered into a string, character by character and how data is obtained from a string character by character.
Example(1):

/* Program demonstrate how string are accessed
Data entered in a string character by character and displayed character by character */

This program is similar to that used for accessing integer arrays. Here ‘\0’ is automatically added to str_name after end of Block 1.

Strings have a wide use in text manipulation. Hence a number of other methods are provided for easy access of strings since we know that ‘\0’ is the terminating character of any string. We can simplify block 2.

‘C’ provides a special format specifier, “%s” for inputting a string and displaying it, with the help of which entire string can be accessed instead of going character by character. Block 1 can be change to

scanf (“%s”, str_name);

Block 2 can be changed to printf (“%s”, str_name); counter ‘j’ is not required in this case.

The drawback of scanf and printf is that they can handle only one word at a time. The function gets and puts, discussed earlier can be used as an alternative, they can handle multiword strings.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics