Computer Programming C: UNIT II: Arrays and Strings

Array of Characters (Strings)

C Programming

In general, a strings in a sequence of character treated as one unit. A string constant is a one dimensional array of characters terminated by a null ('\0') character.

ARRAY OF CHARACTERS (STRINGS)

In general, a strings in a sequence of character treated as one unit. A string constant is a one dimensional array of characters terminated by a null ('\0') character.

Example: char name[ ] = {'V', 'R', 'B', '\0'};

Where, '\0' is a null character and specifies end of the string. Terminating the string with null '\0' is important, because it is the only way the functions that work with a string can know where the string ends.


The last character is called '\0' null character, it looks like two characters but it is actually one character. Each character in the array occupies one byte memory and the last character is always '\0'.

C provides a shortcut method initialising strings.

Example: char name[] = "VRB";

Note that in this declarations '\0' is not necessary. 'C' insert '\0' automatically at the end of the string.

Example: char name[] = "COMPUTER";

The memory representation of above initialization is shown here.


Example 1: Program to print array of characters.

/* Program using array of characters */

#include <stdio.h>

#include <conio.h>

main()

{

      /* Local definitions */

      char name[]="VRB";

      int i=0;

      /* Statements */

      while(i<=2)

      {

            printf("%c", name[i]);

            i++;

      } /* while */

} /* main */

OUTPUT

VRB

EXPLANATION: In this program, we have initialised a character array, and print the string character by character using the while loop.

Example 2: Program to print the character string from the array.

/* Program using array of characters */

#include <stdio.h>

main()

{

      /* Local definitions */

      char name[]="VRB";

      printf("%s", name);

}

#include <stdio.h>

main ()

{

      /* Local definitions */

      char name[] = "VRB";

      int i=0;

      /* Statements */

      while(name[i]!= '\0')

      {

            printf("%c", name[i]);

            i++;

      } /* whiles */

} /* main */

OUTPUT

LAK

EXPLANATION: Here every character in an array is checked whether it is a null character are not. If it is a null character then the control is transferred to the outside of the loop, that is the end of the program. Otherwise it continues with printing.

 

Computer Programming C: UNIT II: Arrays and Strings : Tag: Computer Science : C Programming - Array of Characters (Strings)


Computer Programming C: UNIT II: Arrays and Strings



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation