Home | Hindi | Kabir | Poetry | Workshop | BoloKids | Writers | Contribute | Search | Contact                                                 Shop Online

  News
Channels
In Focus

Analysis  
Bolography  
Cartoons
Environment   
Opinion 

Columns
 Business
 My Word 
 PlainSpeak 
 Random Thoughts 
Our Heritage

Architecture
Astrology
Ayurveda
Buddhism
Cinema 
Culture
Dances
Festivals
Hinduism
History  
People  
Places 
Sikhism
Spirituality 
Vastu 
Vithika  

Society & Lifestyle

Family Matters 
Health
Parenting
Perspective 
Recipes
Society
Teens 
Women 

Creative Writings

Book Reviews
Ghalib's Corner
Humor
Individuality
Jagoji
Literary Shelf 
Love Letters  
Memoirs
Musings
Ramblings
Stories
Travelogues

Computing
  General Articles
 
CC++ 
  Flash 
  Internet Security 
 
Java 
 
Linux     
  Networking  

Computing | CC++   
Handling of Character Strings - 2

We would start here, with the arithmetic operations on characters and strings. C allows us to manipulate characters the same way as we do with numbers. Whenever a character constant or character variable is used in an expression, it is automatically converted into an integer value by the system. The integer value depends on the local character set of the system.

For example, if the machine uses ASCII representation, then,

m = 'a';

printf("%d\n",m);

will display the number 97 on the screen.

Arithmetic operations can also be performed on the character constants and variables.

For example,

s = 'z' - 1;

will cause 1 to be subtracted  from the ASCII value of 'z'(122) and the resultant value of 121 to be assigned to s.

The C library supports functions for converting strings into their corresponding integer values.

The function is of the form

d = atoi(string);

Putting strings together

We cannot apply the arithmetic addition for joining of two or more strings in the manner

string1 = string2 + string3; or

string1 = string2 + "SACY";

For carrying out the above we need to write a program to copy the contents of the string2 & string3 into string1 one after the other. This process is called concatenation of strings.

We now see some functions provided by the C library to overcome the above problem & also provide additional features.

 strcat() Function

strcat() joins two or more strings together. It takes the following form

strcat(string1, string2);

string1 and string2 are character arrays. When the above function is executed, string2 is appended to string1. It does so by removing the null character at the end of string1 and placing string2 from there. The string at string2 remains unchanged.

strcat function may also append a string constant to a string variable. The following is valid

strcat(part1,"SACY");

C also permits nesting of strcat functions. For example

strcat(strcat((string1,string2),string3);

is allowed and concatenates all the three strings together. The resultant string is stored in string1.

strcmp() Function

The strcmp function compares two strings identified by the arguments and has a value 0 if they are equal. If they are not, it has the numeric difference between the first non-matching characters in the strings. It takes the form:

strcmp(string1, string2);

string1 and string2 may be string variables or  string constants. For example

strcmp(name1,name2);

strcmp(name1,"JOHN");

strcpy() Function

The strcpy function works almost like a string-assignment operator. It takes the form

strcpy(string1,string2);

and assigns the contents of string2 to string1. string2 may be a character array variable or a string constant. For example

strcpy(city,"DELHI");

will assign the string "DELHI" to the string variable city. Similarly the statement

strcpy(city1, city2);

will assign the contents of the string variable city2 to the string variable city1.The size of the array city1 should be large enough to receive the contents of city2.

strlen() Function

This function counts and returns the number of characters in a string

n = strlen(string);

Where n is an integer variable, which receives the value of the length of the string. The argument may be a string constant. The counting ends at the first null character.     

– Sachin Mehta
May 1, 2001

C/C ++        
Managing Input-Output Operations
     
Managing Output Operations
    
Decision Making And Branching
     
Decision Making and Branching (if statement)
     
Decision Making and Looping
    
The Do Statement
   
Arrays
    
Arrays - Multi-Dimensional
    
Handling of Character Strings
    
Handling of Character Strings - 2  
User Defined Functions   
   User Defined Functions...contd    
Handling of Functions  

Computing
Flash | Internet Security 
Java | Linux | Networking  

General Articles  

Top | Previous | Next
 


 

Recommend This Page!

Analysis | Architecture | Astrology | Ayurveda | Book Reviews | Buddhism | Cartoons | Cinema | Computing | Culture | Dances
Environment | Fables | Family Matters | Festivals | Hinduism | Health | History | Home Remedies | Humor | Individuality | Jagoji
Literary Shelf | Memoirs | Musings | Opinion | Parenting | Perspective | Photo Essays | Places | Ramblings
Random Thoughts | Recipes | Sikhism | Society | Spirituality | Stories | Teens | Travelogues | Vastu | Vithika | Women

Home | Hindi | Bolography | BoloKids | Kabir | Poetry | Quotes | Workshop | Writers | Contribute | Search | Contact


Boloji.com is owned and managed by Boloji Media Inc

Privacy Policy | Disclaimer
No part of this Internet site may be reproduced without prior written permission of the copyright holder.