Professional Documents
Culture Documents
Character Array
Character Array
Character Array
A string is a group of characters of any length . C++ does not provide string as a data type rather
it implements string as a single dimensional character array . A character array /string is defined
as an array of type char which is terminated by the null character ‘\0’. The (‘\0’) is a character
literal which is an escape sequence.
Declaration of character array
A character array can be declared using the syntax data type array name[size];
The declaration includes the data type char along with name of the array and the maximum size.
For example , if a string is declared as - char name[20];
Although the above array can hold maximum 19 characters(one reserved for ‘\0’), but the
maximum size does not always have to be fully used. So while declaring the character array , the
size should be decided considering the maximum length of the data item it can hold.
Initialization of string
A string of characters can be initialized on declaration by enclosing the characters in double
quotes. The null character will be automatically appended.
char str[] = “Hello”;
The array can also be initialized character by character
char str[] = {‘H’,’e’,’l’,’l’,’o’} ;
or
str[0]=’H’;
str[1]=’e’;
str[2]=’l’;
str[3]=’l’;
str[4]=’o’;
str[5]=’\0’;
Accepting character array from the user
Although string is an array, it is considered as one unit of data item and it is read by the name of
the array (like a variable). The input of the string can be done using cin statement
Example char str[10]; cin>>str;
Although cin can be used to accept the string data from the user, but it cannot be used to accept
any string with a space as a part of it . It is because cin takes space as the data entry
terminator. It will ignore the characters after space , if any. Hence cin be used to accept any
string which has one word in it.
The string can also be accepted from user using the following library functions like :
i. gets() –
This helps to input a string and stores it in the given string/array. The header file to be
used is stdio.h
Syntaxgets(name of the string/character array);
Example
char name[10];
gets(name);
gets() can read a string with ‘ ‘ as a part of it. At the end of the data entry, when user
presses enter , a ‘\0’ character will be added to its end automatically. The ‘\0’ character
indicates end of the string.
In the above example gets() will accept maximum 9 characters as one character will be
reserved for ‘\0’. gets() will automatically add ‘\0’ to a string when enter key is pressed at
the end of data entry.
include<iostream.h>
#include<stdio.h> //for gets()
#include<string.h>
#include<ctype.h> // header file for toupper()
#include<conio.h>
void main()
{
char str1[40],str2[40],str3[80], ans;
int i, k, j, f=0, cnt=0,res,ch;
cout<<"Enter the first string";
gets(str1);
cout<<"Enter second string";
gets(str2);
do
{
cout<<"1 : To concatenate two strings \n";
cout<<"2 : To compare two strings(strcmp)\n";
cout<<"3 : To compare two strings(strcmpi)ignoring the case\n";
cout<<"4 : To search for a substring in a string\n";
cout<<"Enter choice - ";
cin>>ch;
switch(ch)
{
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[40], str2[40],str3[80], ans, temp;
int i,k,j,f=0,cnt=0,res,ch, l=0;
cout<<"Enter the string - ";
gets(str1);
for(i=0;str1[i]!='\0';i++) //loop to count length
{
l++;
}
do
{
cout<<"1 : Copy the contents of a string in another string \n";
cout<<"2:Reversing a string (in the same string)\n " ;
cout<<"3 : Reversing the string using another string\n";
cout<<"4 : Check if the string is palindrome \n" ;
cout<<"5 : Search and count a user given character in a string \n";
cout<<"Enter choice - ";
cin>>ch;
switch(ch)
{
case 1 : for(i=0;str1[i] !='\0';i++) //making a copy
{
str2[i]=str1[i];
}
str2[i]='\0';
cout<<str2;
break;
Program Write a program to accept a string a. Reverse each word and store it in string b
#include<iostream.h>
#include<stdio.h>
#include<string.h>
void main()
{
int c=0,k,pos=0,j=0;
char a[20],b[20];
gets(a);
int l=strlen(a);
for(int i=0;i<=l;i++)
{
if((a[i]==' ')||(a[i]=='\0'))
{
for(k=i-1;k>=pos ;k--)
{
b[j]=a[k];
j++;
}
b[j]=' ';
j++;
pos=i+1;
}
}
b[j]='\0';
cout<<"new string "<<b;
}
Program
Write a program to accept two strings (a and b) from the user. Count how many times string b
appears in a .
#include<stdio.h>
#include<string.h>
void main()
{
int i,k,ctr=0,j=0;
char a[20],b[20];
cout<<"Enter the string: ";
gets(a);
cout<<"Enter the word to be found: ";
gets(b);
for(int l1=0; b[l1]!=’\0’;l1++);//loop to get length of the string b
for(i=0,j=0;s1[i]!='\0';i++)
{
if(s1[i]==s2[0]) //if the first letter of b match with any letter of a
{
for(j=0;s2[j]!='\0';i++,j++) //compares the rest of the lettersof both
{
if(s1[i]!=s2[j]) //
break;
}
i--;
if(j==l1) //if j is equal to the length of b match found
ctr++;
}
}
cout<<"The word occurs "<<ctr<<" times.";
}
/* alternative logic using string functions
#include<iostream.h>
#include<stdio.h>
#include<string.h>
void main()
{
int k,ctr=0,j=0;
char a[20],b[20],c[20];
gets(a);
gets(b);
int l=strlen(a);
int l1=strlen(b);
for(int i=0;i<l;i++)
{
for( j=0,k=i;j<l1;j++)
{
c[j]= a[k++];
}
c[j]='\0';
if(strcmp(b,c)==0)
ctr++;
}
if(ctr==0)
cout<<"Not found";
else
cout<<"Found"<<pos<< " times";
}
*/
Output Questions
1. Give the output of the following code:
char s[20]=”GOODLUCK”;
for(int x=7;x>=0;x--)
{
for(int y=0;y<=x;y++)
cout<<s[y];
cout<<endl;
}
Solution
GOODLUCK
GOODLUC
GOODLU
GOODL
GOOD
GOO
GO
G
2. void main()
{
char text[] ="pOwERALone";
char c = '*';
for(int k=0; text[k] !='\0'; k++)
if (text[k] >='K' && text[k] <= 'P')
text[k] = text[k+1];
else if (text[k] =='E' || text[k] =='e')
text[k] = c;
else if (k %2 ==0)
text[k] = c;
else text [k] = text[k] +1;
cout<<"changed string - "<<text;
}
Output will be : changed string- *w***Bop**