1) String initialization can be carried out in the following ways, similar to that of an array :

Similar documents
ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Variables, Constants, and Data Types

Chapter 4: Computer Codes

VB.NET Programming Fundamentals

Symbol Tables. Introduction

Sources: On the Web: Slides will be available on:

C++ Programming Language

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Informatica e Sistemi in Tempo Reale

Introduction to Java. CS 3: Computer Programming in Java

Lecture 22: C Programming 4 Embedded Systems

How To Write Portable Programs In C

10CS35: Data Structures Using C

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT COURSE CURRICULUM. Course Title: Advanced Computer Programming (Code: )

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Number Representation

Introduction to Data Structures

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Introduction to Java

DATA STRUCTURES USING C

The C Programming Language course syllabus associate level

C Compiler Targeting the Java Virtual Machine

Passing 1D arrays to functions.

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

eggon SDK for ios 7 Integration Instructions

Pseudo code Tutorial and Exercises Teacher s Version

Java Interview Questions and Answers

5 Arrays and Pointers

Comp151. Definitions & Declarations

Answers to Review Questions Chapter 7

Illustration 1: Diagram of program function and data flow

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

1 Abstract Data Types Information Hiding

Project: Simulated Encrypted File System (SEFS)

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

The use of binary codes to represent characters

C++ INTERVIEW QUESTIONS

Data Structures and Data Manipulation

C Strings and Pointers

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Lecture 5: Java Fundamentals III

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

public static void main(string[] args) { System.out.println("hello, world"); } }

C++ Wrapper Library for Firebird Embedded SQL

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

El Dorado Union High School District Educational Services

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

An Overview of Java. overview-1

Data Tool Platform SQL Development Tools

SQL is capable in manipulating relational data SQL is not good for many other tasks

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Stacks. Linear data structures

Basics of I/O Streams and File I/O

Exercise 1: Python Language Basics

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

Pemrograman Dasar. Basic Elements Of Java

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

So far we have considered only numeric processing, i.e. processing of numeric data represented

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

C++ Language Tutorial

Tibetan For Windows - Software Development and Future Speculations. Marvin Moser, Tibetan for Windows & Lucent Technologies, USA

The Power of CALL SYMPUT DATA Step Interface by Examples Yunchao (Susan) Tian, Social & Scientific Systems, Inc., Silver Spring, MD

An Incomplete C++ Primer. University of Wyoming MA 5310

Lab Work 2. MIPS assembly and introduction to PCSpim

Using SQL Server Management Studio

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

BCS2B02: OOP Concepts and Data Structures Using C++

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Example Programs for PLC Fatek equipped with FBs-CMGSM

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

PES Institute of Technology-BSC QUESTION BANK

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Unit 6. Loop statements

ISIS Application Program Interface ISIS_DLL User s Manual. Preliminary Version

Chapter 2: Elements of Java

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Project 2: Bejeweled

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

File Handling. What is a file?

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Chapter 13 Storage classes

Encoding Text with a Small Alphabet

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Oracle Database: SQL and PL/SQL Fundamentals

MATLAB: Strings and File IO

Chapter 13 - The Preprocessor

Using C to Access Data Stored in Program Memory on the TMS320C54x DSP

Lab Experience 17. Programming Language Translation

irods and Metadata survey Version 0.1 Date March Abhijeet Kodgire 25th

The programming language C. sws1 1

Transcription:

Strings in C : Overview : String data type is not supported in C Programming. String is essentially a collection of characters to form particular word. String is useful whenever we accept name of the person, address of the person, some descriptive information. We cannot declare strings using string data type in C since there is no such data type available in C. Instead, an array of type character is used to represent a string. Normally a group of alphabets enclosed with in double quotes is a string literal in C. Description : Strings in C are generally represented by an arrays of characters. The end of the string is marked using a special character, the null character, which is simply the character with the value 0. (The null character has no relation except in name to the null pointer. In the ASCII character set, the null character is named NULL.) It is simply used to denote the end of the string. So all the rules applicable to array is applicable to strings. 11.1 Initialization of a string: 1) String initialization can be carried out in the following ways, similar to that of an array : The following declaration and initialization creates a string containing the word "Sourcelens". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Sourcelens". char Name[11] = {'S', 'o', 'u', 'r', 'c', 'e', 'l', 'e', 'n','s','\0'}; 2) If a string is initialized similar to an array, then the above statement can be rewritten as follows : char Name[11]="Sourcelens"; In this case, it is not necessary to place the null character at the end of a string constant. The C compiler automatically places the '\0' at the end of the string when it initializes the array. 3) Another way to initialize a string is a variation of the above initialization where the size of the array does not have to be specified. char Name[]="Sourcelens"; When we declare char as string[], memory space will be allocated as per the requirement during execution of the program. 11.2 Library functions for string manipulation : The string.h header file contains various functions for manipulating arrays of characters.some of the string functions available are :

String Function strcpy() strcmp() strcat() strlen() strrev() Description String copy function which copies the contents of the source string to the destination string. String comparison function which compares two strings, string1 and string2 and returns a value =0, if string1 = string2 >0, if string1 < string2. <0, if string1 > string2. Concatenates or joins two strings. String function to find the number of characters in a string. String function to reverse the contents of a given string. 11.3 Array of Strings : Another common application of two dimensional arrays is to store an array of strings. A string is an array of characters; so, an array of strings is an array of arrays of characters. Of course, the maximum size is the same for all the strings stored in a two dimensional array. It is possible to declare a two dimensional character array of MAX strings of size SIZE as follows: char Name[MAX][SIZE]; Now, Name is a character array of size MAX * SIZE is created which can store MAX strings of SIZE size each. Figure 11.1

The above figure 11.1 shows a program to initialize a character array with a string and the print the individual characters in the array along with their position in the array. Figure 11.2 The above figure 11.1 shows a program to which denotes the functionalities of the library string functions such as strcpy(), strcat() and the strcmp() functions respectively. The strcpy() function copies the contents of the source (second argument ) to the destination array(first argument i.e array name). The strcat() function concatenates the content of the second argument with the first argument and the strcmp() function performs a character by character comparison of the two strings given as input.

Figure 11.3 The above figure 11.3 shows an example to sort an array of 5 strings using the bubble sort technique. The program shows the transition of the first iteration in the watch window. Since the first string in the array is greater than the second string, we use a temporary variable to swap the two values after comparing the two strings using the strcmp() function. Figure 11.4

The above figure 11.4 shows an output of the sorting function which sorts the given list of strings in the two dimensional array in the alphabetical order as seen in the watch window. The given array of strings are sorted with the help of a temporary character array and the bubble sort technique is implemented for sorting. 11.4 UNICODE : Not all the language has 26 letters. Some languages like Japanese or Chinese have 1000s of characters and so 256 numbers are not enough to represent all of them. All the strings seen so far are character arrays or arrays of numbers of size 8 bits. ( 1 byte ). 8 bit can only hold up to 2^8 values which is 256 values. So a 16 bit scheme came in to being called UNICODE. It is the modern way to represent strings. All major OS, programming languages, support and recommend Unicode instead of 1 byte char including C.