If you like my posts please leave a comment.

Sunday, September 9, 2012

STRING AND CHARACTER RELATED FUNCTIONS


In C++ string is a null-terminated array of characters. To manipulate strings in C++ we need the help of string functions and this requires including the header file string.h in order to include function prototype and definition. To manipulate characters we need to use character functions and this requires including ctype.h header file in your program.

Character Functions:

Here by below I am including some of the important character functions with descriptions:
Function
Description
Example
int isalnum(int ch)
The function isalnum() returns non-zero if its argument is a letter or a digit. If the character is not an alphanumeric isalnum() returns 0 (Zero)
char ch;
if(isalnum(ch))
cout<<”\n Alphanumeric”;
else
cout<<”\n Non-Alphanumeric”;
int isalpha(int ch)
The function isalpha() returns non-zero if ch is a alphabet, else returns zero.
char ch;
if(isalpha(ch))
cout<<”\n An alphabet”;
else
cout<<”\n No an alphabet”;
int isdigit(int ch)
This functions returns non-zero if ch is a digit (0 – 9) else returns zero.
char ch;
if(isalpha(ch))
cout<<”\n Is a digit”;
else
cout<<”\n Not a digit”;
int isxdigit(int ch)
Tests for hexadecimal character.
isxdigit is a macro that classifies ASCII-coded integer values by table lookup. You can make this macro available as a function by un-defining (#undef) it.
int main(void)
{
  char ch = 'F';
  if (isxdigit(ch))
    cout<<ch<<" is a hexidecimal digit";
  else cout<<ch<<"\n is not a hexidecimal digit";
   getch();
  return 0;
}
Output:
int isupper(int ch)
Function isupper() returns non-zero if ch is in upper case; else returns zero.
char ch;
if(isupper(ch))
cout<<”\n Is in upper case”;
else
cout<<”\n is in lower case”;
int islower(int ch)
Function islower() returns non-zero if ch is in lower case; else returns zero.
char ch;
if(islower(ch))
cout<<”\n Is in lower case”;
else
cout<<”\n is in upper case”;
int isspace(int c)
isspace is a macro that classifies ASCII-coded integer values by table lookup
char ch = ' ';
if (isspace(ch))
cout<<ch<<"is a white space";
else cout<<ch<<"is not white space\n";
Output:
 is a white space
int toupper(int ch)
Function toupper(), return upper case equivalent to ch; otherwise keeps the character unchanged.
char ch;
cout<<toupper(ch);
int tolower(int ch)
Function tolower(), returns lower case equivalent to ch; otherwise keeps the character unchanged.
char ch;
cout<<tolower(ch);
int toascii(int ch)
toascii is a macro that converts the integer c to ASCII by clearing all but the lower 7 bits; this gives a value in the range 0 to 127
char ch;int result;
ch ='^';
result = toascii(ch);
cout<<"\n ASCII of "<<ch<<" = "<<result;
Output:
ASCII of ^ = 94
 
Note** All above mentioned character function uses ASCII equivalent of the passed character to compare or to convert. That is why on all above functions passed argument is shown as of type int.
 

String Functions:

The table given below summarizes some of the important string functions used in C++. To use these functions you need to include string.h header file.

String Functions:

The table given below summarizes some of the important string functions used in C++. To use these functions you need to include string.h header file.
Function
Description
Example
char *strcat(char *str1, const char *str2)
The function strcat(), concatenates a copy of str2 to str1 and terminates str1 with a null character. Remember str1 should be large enough to hold both, its original content and the contents of str2.
char *str1="Computer";char *str2=" Science";
strcat(str1,str2);
cout<<str1<<endl;
Output:
Computer Science
int strcmp(const char *str1, const char *str2)
The function strcmp(), alphabetically compares two strings passed as arguments. It returns –ve value if str1 is less than str2; 0 if str1 and str2 are equal; and returns +ve value if str1 is greater than str2.
const char *str1="Abc";
const char *str2="Def";
cout<<strcmp(str1,str2)<<endl;
cout<<strcmp(str2,str1);
Output:
-3
3
char *strcpy(char *str1, const char *str2)
Function strcpy(), copies the content of str2 into str1.
char *str1=" ";
const char *str2="C++";
strcpy(str1,str2);
cout<<str1<<endl;
Output:
C++
int strlen(const char *str)
Function strlen(), returns the length of the null-terminated string pointed by str. The null character is not counted.
const char *str="C++";
cout<<strlen(str)<<endl;
output:
3
char *strrev(char *s)
Reverses a string.
strrev() function changes all characters in a string to reverse order, except the terminating null character
int main(void)
{
   char *mystr = "string";

   cout<<"Before strrev(): "<<mystr;
   strrev(mystr);
   cout<<"\nAfter strrev(): "<<mystr;
   getch();
   return 0;
}
Output:
char *strset(char *s, int ch)
Sets all characters in a string to a given character.
strset() function sets all characters in the string s to the character ch. It quits when the terminating null character is found.
int main(void)
{
   char string[10] = "123456789";
   char symbol = '*';

   cout<<"Before strset(): "<<string;
   strset(string, symbol);
   cout<<"\nAfter strset(): "<<string;

   getch();
   return 0;
}
Output:

No comments:

Post a Comment