0
713views
What is a String?

Subject : Structured Programming Approach

Title : Arrays, String, Structures and Union

Marks : 5M

1 Answer
0
0views

A string is a array of characters. Any group of characters(except the double quote sign) defined between double quotation marks is a constant string.

Eg: 1) “Man is obviously made to think”

If we want to include a double quote in a string, then we may use it with the back slash.

Eg: printf(“\”well done!\””);

will output “well done!”

The operations that are performed on character strings are

1.Reading and writing strings.

2.Combining strings together.

3.Copying one string to another.

4.Comparing strings for equality.

  1. Extracting a portion of a string.

Declaring And Initializing String Variables

A string variable is any valid C variable name and is always declared as an array.

The general form of declaration of a string variable is char string_name[size]; Eg: char city[10]; char name[30];

When the compiler assigns a character string to a character array, it automatically supplies a null character (‘\0’) at the end of the string. Therefore, the size should be equal to the maximum number of characters in the string plus one. C permits a character array to be initialized in either of the following two forms

static char city[9] = “NEW YORK”;

static char city[9] = {‘N’, ‘E’, ‘W’, ‘ ‘, ‘Y’, ‘O’, ‘R’, ‘K’, ‘\0’};

Reading Words

The familiar input function scanf can be used with %s format specification to read in a string of characters.

Eg: char address[15];

scanf(“%s”,address);

Please log in to add an answer.