Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Handling of Character Strings

Posted By: Hildemare Miller     Category: C Programming     Views: 1823

This article explains about handling of character strings in C programming.

-  A string is an array of characters in C
-  Any group of Chars in “” is a string const
-  \” for including double quotes in const
-  Common Operations performed are
  • Reading and writing
  • Combining, copying and comparing
  • Extracting portion of it


Declaring and Initializing

  •  char string_name[size]
  •  Here size should be no of characters + 1
  •  While supplying a string to a char array, compiler auto adds null char  at the end
  •  char city[5]= “Surat”
  •  char city [5]={‘S’,’u’,’r’,’a’,’t’,’\0’}
  •  Size can be omitted while initializing

Writing Strings using %s and %w.ds

  • %s and %w.d s
  • When w is zero, nothing gets printed
  • The runtime provision for w and d with printf( “%*.*s”, w,d, string) specification
  • It’s handy in printing sequence of characters for a given string in style
  • Minus sign for left justification


Arithmetic Operation on Chars using atoi() function

  •  char ch=‘a’; printf (“%d”,ch) will print 97
  •  x = ‘z’ – 1 assigns 121 to x
  •  ch >=‘A’ && ch <=‘Z’
  •  x= character – ‘0’ converts a character digit to it’s equivalent digit
  •  atoi is a function which converts a string of digits to a number


Putting String Together

  •  string1 = string 2 is invalid!
  •  string 3 = string1 + string2 is invalid!
  •  Need to write special routines to concatenate two or more strings
  •  if (name1 == name2) or (name1 = “Jay”) are invalid expressions for  string
  •  C has a rich set of string manipulation functions 

Few String Handling Functions

table


strcat(str1,str2) Statement

  •  part1 = very \0  part2 = good\0 
  •  part3 = bad\0
  •  After Execution of strcat(part1,part2)
  •  part1= very good\0   part2 = good\0
  •  str1 must be large enough!
  •  Nested strcat like strcat(strcat(str1,str2),str3 ) is allowed, result  is stored in str1

strcmp() Function

  • It compares two strings identified by the arguments and returns 0 if they are equal
  • If they are not, it has the numerical difference between the first non matching characters in the string
  • strcmp(“their”,”there”) will return –9 because it’s the difference between ASCII value of i and ASCII value of r


strcpy() Function

  • It acts as a string assignment operator
  • It takes the form strcpy(string1,string2)
  • It assigns the contents of string2 to string1
  • string2 can be a string constant like strcpy(string1,”Delhi”)
  • strcpy(city1,city2) will assign contents of city2 to city1

strlen() Function

  • This function counts and returns no of characters in a string
  •  n = strlen(string1)
  • Here n is an integer variable which receives the value of length of string
  • The argument may be a string constant
  • The counting ends at the first null character

Table of Strings

  • List of names of employees of a company, list of names of students etc are needed
  • student[30][15] can be used to store 30 student names of max 15 chars
table


Easier Way to Store Names

 static char city[][]
{“Chandigarh”,
“Madras”
“Ahemdabad”,
“Hydrabad”
“Mumbai”
};
  
Share: 


Didn't find what you were looking for? Find more on Handling of Character Strings Or get search suggestion and latest updates.

Hildemare Miller
Hildemare Miller author of Handling of Character Strings is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!