Saturday, October 30, 2010

character or char arrays as data structures in c: string reverse program and palindrome program using c programming

c programming arrays ---> data structures in c: char arrays in c programming
C programming arrays:
Arrays are important data structures in C.
Arrays are very much useful in situations when we need to store many variables of same data type and treat them similarly. For understanding c programming arrays and arrays as data structures in c, read the hub “Array Basics in C programming”
I am explaining here only character arrays in c programming.
What is an array?
An array is collection of similar type of data items.
And a character array is called a string.
Character arrays in C programming have a different concept from integer arrays or float arrays in C. Character arrays are declared in the same way as integer or float arrays but one thing that is different in character arrays is that a NULL character (‘\0’) is appended automatically at the end of the character array.
char arr[10];
the above statement declares a character array named arr with 10 continuous memory locations each occupying 1 byte because the base size of a character data type is 1 Byte. So the above array takes 10 Bytes in memory.
Now a string (group of character or a character array is called a string) is input in array by the following statement.
cin>>arr;
In the above input statement the string does not contain any whitespace because cin>> ends the string where a whitespace is encountered. If whitespaces are to be input then the following statement can be used
gets(arr);
do not forget to include stdio.h file to let the gets() function work
Suppose if the user inputs “HELLO” from the keyboard. The actual storage in memory would be :

c programming arrays: arrays as data structures in c : sample programs

A NULL Character(‘\0’) or string terminator is automatically appended after the last character of the string. It (‘\0’) marks the end of the string. This does not happen in integer or float arrays in C++.
Example 1:

c programming arrays: char arrays as data structures in c: sample program to reverse a string and count number of characters in string

/* program to input a string then count number of characters in string and

print the reverse of the string*/

#include<stdio.h> //for printf , scanf and other functions

#include<conio.h> //for clrscr() and getch()functions

void main()

{

clrscr();

char arr[10];

printf("Enter A String\n");

gets(arr);

for(int i=0;arr[i]!='\0';i++);//empty loop which does not have a body

printf("Length of the string is %d", i);

//now reverse print of the string

for(i--;i>=0;i--)

printf("%c",arr[i]);

getch();

}

c programming arrays: char arrays as data structures in c: sample program 1 output

OUTPUT:
Enter A String
MADAM
String is palindrome
Example 2:

c programming arrays: char arrays as data structures in c: sample program to check a string is palindrome or not

/*Program to input a string then check whether string is palindrome or not
A palindrome string is same as its reverse like RADAR, NITIN, MADAM */

#include<stdio.h>  //for printf, scanf,gets  and other functions
#include<conio.h>  //for clrscr() and getch()functions
void main()
{
clrscr();

char arr[10];
int i,j,flag=1;
printf("\n Enter A String\n");

gets(arr);

for(i=0;arr[i]!='\0';i++);//empty loop which does not have a body
for(j=0,i--;i>=0;i--,j++)//i moves backward and j moves forward
{
 if(arr[i]!=arr[j])
 {
  flag=0;
  break;
 }
}
if(flag==1)
printf("String is palindrome");
else
printf("String is not palindrome");

getch();
}

c programming arrays: char arrays as data structures in c: sample program 2 output

OUTPUT:
Enter A String
MADAM
String is palindrome

No comments:

Post a Comment