homecode zoneQuick Codedownloadssubmit codeabout usfeedbackscontact ussitemap
 
Beginners
Intermediate
Proffessional
Array
Array & String
Looping
Branching
Constant Variable & Data type
Operators
Function
Structures & Unions
Pointers
Liabrary
Pro - Languages
C/C++
Java
Visual basic
asp
vb.net 2003/2005
asp.net
Quick Code
SUBMIT CODE
Mail List
LINKS
Submit LINK
 
ProgrammerGuide.net - C-Arrays and String.
Home » C/C++ Zone » C-Array & String

Introduction

A string is a sequence of characters that is treated as a single data item. We have used strings in a number of examples in a number of example in the past. Any group of characters (except double quote sign) defined between double quotation marks is a string constant. Example: "Man is obviously made to think."

For example,
     printf ("\" Well Done !"\");

will output the string
     " Well Done !"

While the statement
     printf (" Well Done !");

will output the string
     Well Done!


1. List of temperatures recorded every hour in a day, or a month, or a year
2. List of employees in an organization.
3. List of products and their cost sold by a store.
4. Table of daily rainfall data and so on.

Character  strings are often used to build meaningful and readable programs. The common operations performed on character string include:

1. Reading and writing strings
2. Combining string together
3. Copying one string to another
4. Comparing string for equality
5. Extracting a portion of a string

In this chapter we shall discuss these operations in detail and examine library function that implement them.

Declaring and Initializing String Variable

C does not support strings as a data type. However, it allows us to represent string as character arrays. in c therefore, a string variable is any valid variable name and is always declared as an array of characters. The general form of declaration of a string variable is.
 
char string_name[ size ];

The size determine the number of characters in the string.... name. Some examples are:

char city[10];
char name[30];

Like numeric arrays, character arrays may be initialized when they are declared C permits a character array to be initialized in either of the following two forms

char city [9] = " NEW YORK ";
char city [9] = {' N ',' E ',' W ',' ' ,' Y ',' O ',' R ',' K '};

This will result in a compile time error. Also note that we cannot separate the initialization from declaration. This is

char str3[5];
str3 = "GOOD";

is not allowed. Similarly,

char s1[4] = "abc";
char s2[4];
s2 = s1; /* Error //

is not allowed. An array name cannot be used as the left operand of an assignment operator.



« Previous 1 2 3 Next »

Viewers: 16
Visitors:127290
Last Updated on:Sunday, January 17, 2010
Home | About us | Feedback | Contact us | Sitemap
Copyright © 2008 ProgrammerGuide.net. All rights reserved.
This site is best viewed in IE 6.0, Firefox 1.5 and above with screen resolution of 1024 X 768.