Character Array in c++
About  Contact


Character Array in C++

Introduction: Character array is used to store non numeric infomation. e.g name, address etc, if we want to accept name of a person or address of a person then we need to declare character arrays. When character array is declared one thing to be kept in mind the number of character required to store the non numeric information. We always consider the optimum case. e.g suppose we need an array to accept name of a person then we can consider 20-30 characters, for address we can consider 50-60 characters. Remembered each character occupy one byte of memory so un-necessary do not take very large number.

Syntax of declaration : char name_of_array[ no_of_characters];

e.g char name[10], it will create an array of 10 characterss whose name is "name". Actually in this array "name" we have 10 char variables they are - name[0], name[1], name[2]...name[9] and each of them can store one character.

See the figure below-

character array

The above array is ready to store any "word" or "string" (is collection of characters or words) having 9 characters, the last character is used to store null character represented by '\0' (slash zero). This slash zero makes the array a string otherwise it reamains a ordinary character array only. When we have to accept a string with multiple words then we use an input function gets() pronounced as get-string, there is a output function puts() is used to display a string on the screen, similar to cout. Both the functions are defined in <stdio.h>. "cin" is used to accept string having single word only because "cin" treminates on space or new line (enter key)
Syntax of gets(): gets(name)- accepts a string from user through keyboard.
Syntax of puts(): puts(name) - displays a string on the monitor.
Now let us write a progran to accept a word and display it
#include <iostream.h>

#include <stdio.h>
char name[10];

void main(){

   cout<<"Enter a word :";
   gets(name);
	cout<<name;
}
Let us run the above program -
Enter a word :INDIA
Internally the array would be as below
filled char array
The word "INDIA" will be displayed on the screen. Here you see the last character in the array is '\0' which makes the "name" a string. A character array can be assigned to a string at the time of declaration only as it is done in case of numeric array.
char s[]="this is C++"; is allowed
But it can not be done as-
char s[20];
s="This is C++"; not allowed.
When we write 'd' it represent an ordinary character but wenn we write "d" it represent a string with one character.
Before we write programs with strings let us study few frequently used string functions-
Function Header file What it does
strlen(s) string.h It returns number of character (including space) in the string "s"
strcpy(d,s) string.h It copies string "s" to string "d"
strcmp(s1, s2) string.h It returns zero if string "s1" and "s2" are equal (case sensitive) otherwise it returns a non zero value.
strcmpi(s1, s2 string.h Same as "strcmp()" but not case sensitive
strcat(s1, s2) string.h joins string "s2" behind string "s1"
strrev(s) string.h It reverses the string "s", if s="abc" then after applying this function "s" becomes "cba".
isalnum(c) ctype.h Returnsnon zero if character 'c' is an alphabet or a digit otherwise it returns zero value
isalpha(c) ctype.h Returns non zero if character 'c' is an alphabet otherwise it returns zero value
isdigit(c) ctype.h Returns non zero if character 'c' is a digit otherwise it returns zero value
islower(c) ctype.h Returns non zero if character 'c' is a lower case letter otherwise it returns zero value
isupper(c) ctype.h Returns non zero if character 'c' is a upper case letter otherwise it returns zero value
tolower(c) ctype.h Converts the caharacter 'c' to lower case, if it is already in lower case then it remains as it is.
toupper(c) ctype.h Converts the caharacter 'c' to upper case, if it is already in upper case then it remains as it is.
Now let us write few programs using character array-
Q51.Write a program to name and age of a person the display if he can vote or not (age>=18 can vote). Message to be like "Mr. xyx you can Vote"
Q52.Write a program to accept a string then find number of capital letter, small letter, digits in it.
Q53.Write a program to accept a string then check if it is a palindrome or not. ( without using "strrev()" function.
Q54.Write a program to accept a string then replace the capital letters by small letters and vice-versa; also replace spaces by "#" and digits by '@'.
Q55.Write a program to accept a string then count number of words, number of vowels .
Q56.Write a program to accept two strings the concatenate (join) them
Program-51
#include <iostream.h>
#include <stdio.h>
void main(){
  char name[20], i;
  int age;
  cout<<"Enter name :";
  gets(name);
  cout<<"Enter age :";
  cin>>age;
  if(age>=18){
     cout<<name<<" you can Vote";
  }
  else{
     cout<<name<<" you can not Vote";
  }
}
In this program the char array name will accept the entered name through the "gets(name)" statement, number of character should be less equal to 19. Then the message "Mr. xyz can Vote" or "Mr. xyz you can not Vote" will be displayed based on the age.
Vote output

Program-52
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main(){
  char s[25];
  int cap=0, sml=0, dig=0, len, i;
  cout<<"Enter the string:";
  gets(s);
  len=strlen(s);
  for(i=0; i<=len-1; i++){
    if(isupper(s[i])){
       cap++;
    }
    else if(islower(s[i]){
       sml++;
    }
    else if(isdigit(s[i]){
       dig++;
    }
  }
  cout<<"\nNumber of capital letters ="<<cap;
  cout<<"\nNumber of small letters ="<<sml;
  cout<<"\nNumber of digit letters ="<<dig;
}
In this program see that the for loop goes from 0 to len-1, because len gives us the number of characters in the string, but the array index starts from zero. i.e if number of character in the string is 18 then in the array the characters reside in cells from 0 to 17. See the "isupper()" function it is not camparing (isupper(s[i]), in this kind of notation if the returned value of the function is zero then the if" statement generates "true" value. Same is the case of "islower()", "isdigit()" functions.
Program-53
#include <iostream.h>
#include <stdio.h>
#include <string.h>
void main(){
  char s[25];
  int startMark, endMark,len, p=0;
  cout<<"Enter the string:";
  gets(s);
  len=strlen(s);
  startMark=0;
  endMark=len-1;
  while(startMark < endMark){
    if(s[startMark]!=s[endMark]){
       p=1;
       break;
    }
    startMark++;
    endMark--;
  }
  if(p==0){
    cout<<"It is palindrome";
  }
  else{
    cout<<"It is not Palindrome";
  }
}
In this program see one marker (startMark) is taken at the begining of the string and one marker (endMark) is taken at the enf of the string, then the two characters pointing by these two markers are compared, if at any step the two characters are not same then the sting can not be a palindrome hence make "p=1" and exit the loop. See the while loop iterates till "startMark" is less than the endMark, "startMark" goes ahead (increases) "endMark" comes backward (decreases). Ultimately the value of "p" is checked and proper message is displayed. The same program can be done by reversing the string and comparing it with the original string. See below
Program-53 (reversing the string)
#include <iostream.h>
#include <stdio.h>
#include <string.h>
void main(){
  char s[25], s2[25];
  int len, i, k=0;
  cout<<"Enter the string:";
  gets(s);
  len=strlen(s);
  for( i=0; i<=len-1; i++){
     s2[k]=s[i];
     k++;
  }
  s2[k]='\0';
  if(strcmp(s,s2)==0){
    cout<<"It is palindrome";
  }
  else{
    cout<<"It is not Palindrome";
  }
}
In this program see that character array "s2" is copying the characters of "s" in reverse order, then see that at the end of "s2" null character ('\0') is added to make is a string, if '\0' is not added then you can not use "strcmp()". The original and the reverse strings are compared and proper message is displayed ,
Program-54
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main(){
  char s[25];
  int len, i;
  cout<<"Enter the string:";
  gets(s);
  len=strlen(s);
  for( i=0; i<=len-1; i++){
     if(isupper(s[i])){
       s[i]=tolower(s[i]);
     }
     else if(islower(s[i])){
       s[i]=toupper(s[i]);
     }
     else if(s[i]==' '){
       s[i]='#';
     }
     else if(isdigit(s[i])){
       s[i]='@';
     }
   }
	cout<<"The changed string ="<<s;
}
In this program see that space character is compare directly, use single quotes and a single space within it.
Program-55
#include <iostream.h>
#include <stdio.h>
#include <string.h>
void main(){
  char s[25];
  int len, i, vno=0, wno=0;
  cout<<"Enter the string:";
  gets(s);
  len=strlen(s);
  for( i=0; i<=len-1; i++){
     if(s[i]==' '){
       wno++;
     }
     else if(s[i]=='a'|| s[i]=='a'|| s[i]=='A'|| s[i]=='e'|| s[i]=='E'|| 
      s[i]=='i'|| s[i]=='I'|| s[i]=='0'|| s[i]=='O'|| s[i]=='u'|| s[i]=='U'){
       vno++;
     }
   }
	cout<<"The number of words ="<<wno+1<<"\n";
   cout<<"The number of vowels ="<<vno;
}

For counting number of words we have counted space (asuming one space after each word), then the number of words will be "number of space" + 1. The if statement checking for vowel is written in two line, actually you have to write it in single line.
Program-56
#include <iostream.h>
#include <stdio.h>
#include <string.h>

void main(){
  char s1[25], s2[25], s3[50];
  cout<<"Enter the string-1:";
  gets(s1);
  cout<<"Enter the string-2:";
  gets(s2);
  strcpy(s3, strcat(s1,s2);
  cout<<"the concatenated string ="<<s3;
}
In this program see that while we concatenate "s1" and "s2" we can not write s3=strcat(s1, s2), therefore strcpy() function is used which copies the concatenated string to s3. See output below-
String cat



Disclaimer: We have taken otmost care to keep the information error free. The site is not responsible for any kind of damage of whatsoever nature.
©Copy right reserved