Showing posts with label Character Array. Show all posts
Showing posts with label Character Array. Show all posts

Wednesday, May 15, 2013

BlueJ program to enter characters and arrange them according to their ASCII values



In this java program, the user will enter character values and the equivalent ASCII values will be stored in another numeric array. The characters will be arranged in ascending order and display the values.

import java.io.*;
class A
{
 char ch[],c;
 int arr[];
 BufferedReader br;
 int x=0,i,j,t;
 A()
 {
  ch= new char[100];
  arr=new int[100];
  }
  public void take() throws Exception
  {
   while(true)
   {
     br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a character:");
     c=(char)br.read();
     arr[x]=(int)c;
     ch[x++]=c;
     if (x==10)
     break;
     }
    }
     public void show()
     {
      System.out.print("\n\nEntered characters=");
      for(i=0;i< x;i++)
      {
       System.out.print(" "+ch[i]);
       }
       for(i=0;i< x-1;i++)
       {
        for(j=i+1;j < x;j++)
       {
        if(arr[i] >arr[j])
        {
         t=arr[i];
         arr[i]=arr[j];
         arr[j]=t;
         c=ch[i];
         ch[i]=ch[j];
         ch[j]=c;
         }
         }
         }
      System.out.println("\n\nSorted characters with their ASCII values=\n");
      for(i=0;i< x;i++)
      {
       System.out.println(arr[i]+ " "+ch[i]);
       }
      }
      public static void main(String args[]) throws Exception
      {
       A ob=new A();
       ob.take();
       ob.show();
       }
       }

Monday, October 4, 2010

Pointer and character type array


Like integer array pointer can also points to character array.
Here is a sample C program :

#include< stdio.h >
void main ()
{
char str []="This is a string";
char *str2=str;
clrscr ();
printf ("%s\n", str);
printf ("%s\n", str2);
str2++;
printf ("%s\n", str2);
 getch ();
 }



Another such program using C Language

#include< stdio.h >
void main ()
{
char str [20];
char *str2;
clrscr ();
printf ("Enter any string:-");
scanf ("%s", str);
str2=str;
printf ("String displayed through the variable:-%s\n", str);
printf ("String displayed through the pointer:-%s\n", str2);
printf ("\nString displayed character wise through the pointer:-\n");
while (*str2!='\0')
{
printf ("%c\n", *str2);
str2++;
}
 getch ();
 }


Monday, September 13, 2010

Accessing array using pointer in c programming language

Program on accessing array using pointer


In this program we will accept five numbers from the user and store them in an array and the values will be displayed through pointer.

#include< stdio.h >
void main ()
{
int num [5];
int i, *ptr;
clrscr ();
printf ("Enter the five numbers:-");
for (i=0;i< 5;i++)
{
scanf ("%d", &num [i]);
}
ptr=num;
printf ("\nThe numbers are:-");
for (i=0;i< 5;i++)
printf ("%d ", *ptr++);
getch ();
 }


Saturday, August 14, 2010

Two-Dimensional or 2 d character array in C programming


One-dimensional array is fine to store a single string, but to store a number of strings we have to declare two-dimensional character type array. The declaration is simply like 2 d integer type array. Suppose we want to store the name of the months in a character array. We have twelve months in a year and the length of ‘september’ is maximum-9. So the array should be char month[12][10].The first index indicates the number of string to be stored and the second index indicates the number of locations for each string. While storing string in 2 d array we have to mention the first index.

This C program will take the name of the months from the user, store them in 2d array  and the month names will de displayed in upper case.

#include< stdio.h >
#include< string.h >
void main()
{
char month[12][10];
int i,j;
clrscr();
for(i=0;i< 12;i++)
{
printf("Enter the month name:-");
scanf("%s",&month[i]);
}
printf("Month names are:-\n");
for(i=0;i< 12;i++)
{
j=0;
while(month[i][j]!='\0')
{
printf("%c",toupper(month[i][j]));
j++;
}
printf("\n");
}
getch();
 }

Sunday, August 8, 2010

C programs on string to check palindrome


Since the last few posts of our C programming language tutorial, we are continuing on array – character type. Actually string in C Language programming is a vast chapter and needs more time. We’ll see more and more programs on string in this C language programming tutorial.

Another string function is void strcpy (char ch1[],char ch2[]).This function copies one string to another. The first argument is the destination and the second argument is the source. The value in the second array will copied on the first array. If the first array has any value then the value will be lost and it will hold the value of the second argument, while the value in the second array will remain unchanged.

#include< stdio.h >
#include< string.h >
void main()
{
char ch1[50],ch2[50];
int i;
clrscr();
puts("Enter the first string:-");
gets(ch1);
puts ("Enter the second string:-");
gets (ch2);
printf ("Before calling the function the strings are:-\n");
printf ("%s", ch1);
printf ("\n%s", ch2);
strcpy (ch1, ch2);
printf ("\nAfter calling the function, the strings are:-\n");
printf ("%s", ch1);
printf ("\n%s", ch2);
 getch ();
 }
 }

Palindrome checking program on string
.
#include< stdio.h >
#include< string.h >
void main ()
{
char ch1 [50], ch2 [50];
int i, x=0;
clrscr ();
puts ("Enter the string:-");
gets (ch1);
for (i=strlen (ch1)-1; i >=0; i--)
{
ch2[x]=ch1 [i];
x++;
}
ch2[x] ='\0';
if (strcmp (ch1, ch2) ==0)
puts ("The string is palindrome.");
else
puts ("The string is not palindrome.");
getch ();
 }

Saturday, August 7, 2010

C array programs on searching vowels - consonants - words and punctuations


This is the continuation of character type array.  Here is another C program on string.

Take a sentence from the user and count the number of vowels, consonants, punctuations, and words in the sentence.

#include< stdio.h >
#include< string.h >
void main()
{
 char ch [50];
 int i,vowel=0,cons=0,space=0,punc=0,word=0;
 clrscr();
 puts("Enter the string:-");
 gets(ch);
 for (i=0;i< strlen (ch);i++)
 {
 if((ch[i]=='a')||(ch[i]=='e')||(ch[i]=='i')||(ch[i]=='o')||
   (ch[i]=='u')||(ch[i]=='A')||(ch[i]=='E')||(ch[i]=='I')||
   (ch[i]=='O')||(ch[i]=='U'))
    vowel++;
 else if((ch[i]==',')||(ch[i]=='.')||(ch[i]==';')||(ch[i]==':')
            ||(ch[i]=='"')||(ch[i]=='?'))
            {
             punc++;
             word++;
             }
 else if(ch[i]==' ')
            {
             space++;
             word++;
             }
 else
            cons++;
 }
 printf("Total vowels in the string is :-%d.",vowel);
 printf("\nTotal consonants in the string is :-%d.",cons);
 printf("\nTotal spaces in the string is :-%d.",space);
 printf("\nTotal words in the string is :-%d.",word);
 printf("\nTotal punctuations in the string is :-%d.",punc);
 getch();
 }

Thursday, August 5, 2010

C programs on C array to display in reverse order and searching


In the next few posts we will work on C array – char type. We will see Different type of  programs on C array.


C program on array to search a specific character from it

Take a string and a special character from the user and display whether the character is available in the string. If available then how many times ?

#include< stdio.h >
#include< string.h >
void main()
{
 char ch[20],c;
 int i=0,counter=0;
 clrscr();
 puts("Enter the string:-");
 gets(ch);
 puts("Enter the character:-");
 scanf("%c",&c);
 for(i=0;i< strlen(ch);i++)
 {
 if(c==ch[i])
 counter++;
 }
 if(counter!=0)
 printf("The character '%c' occurs %d times in the string.",c,counter);
 else
 printf("The character '%c' is not available in the string.",c);
 getch();
 }

C program to display a string in reverse order

#include< stdio.h >
#include< conio.h >
void main()
{
 char str1[100],str2[100];
 int i=0,n=0,j;
 clrscr();
 printf("\nEnter the string::");
 do{
    str1[i]=getchar();
    i++;
    }while(str1[i-1]!='\n');
    str1[i-1]='\0';
 printf("\nThe string is \t%s",str1);
 printf("\nNow the string will be displayed in reversed order\n");
 for(j=i-2;j >=0;j--)
 {
  str2[n]=str1[j];
  n++;
 }
 str2[n]='\0';
 printf("\nReversed string\t%s",str2);
 getch();
}

C programs on character type array using string functions


There are several string manipulating functions supported by C Programing Language. These functions are available in string.h header file. One of such function is int strlen(char ch[]). This function takes a character array as argument and returns the length of the string. Length of a string means the total number of characters in the character array including blanks.

Here is an program on C Language to demonstrate the above feature

#include< stdio.h >
#include< string.h >
void main()
{
 char ch[20];
 int i;
 puts("Enter your name:-");
 gets(ch);
 i=strlen(ch);
 printf("Your name contains %d characters including space.",i);
 getch();
 }


In the above C program, the user may have entered his/her name with surname and at the end may have put a full stop. So the above program will show the length of the string including the space and the full stop. But actually the length means the total number of characters excluding the space and full stop. Our next C program on string will be a modified version of the above program that will display the number of characters in the name of the user.

C program to calculate length of a string without using library function

#include< stdio.h >
void main()
{
 char ch[20];
 int i=0,length=0;
 clrscr();
 puts("Enter your name:-");
 gets(ch);
 while(ch[i]!='\0')
 {
 if((ch[i]!=' ')&&(ch[i]!='.'))
 length++;
 i++;
 }
 printf("Your name contains %d characters .",length);
 getch();
 }

Note that in this above program string.hheader file is not used to calculate the length of the string.

Thursday, July 29, 2010

character type array in C programming language


Like array of numeric types, it can be of character type in C Language. char type variable hold only one character at a time. But in some situations we have store a sequence of characters in a single variable (e.g. your name is a sequence of characters) and normal char type variable cannot serve the purpose. A sequence of characters is known as string. Here we have to declare a variable of char type.  C syntax of declaring is same as integer or float type array variable. So a character type array variable can be declared as char ch[20] in C Language, where char is the type, ch is the variable name with size 20. In case of character type array with size 20, the array variable can hold maximum 19 characters as at the end of the characters of a character type array there should be a NULL character, which is also known as string terminator. A special character can also denote string terminator ‘\0’.Whenever the char type array is initialized with a string then the NULL character is automatically set in the array but when the array is initialized character by character then the NULL character should be set by the user at the end of the characters.

Program on character type array in C Language.

   #include<stdio.h>
   void main()
{
char ch[]="This is string";
clrscr();
printf("The string is :-%s",ch);
getch();
}

Another program on character type array.

 #include<stdio.h>
 void main()
{
char ch[20];
clrscr();
printf("Enter a string:-");
scanf("%s",ch);
printf("The string is :-%s",ch);
getch();
}  
                                                            
In C Language, if we initialize any character array character wise then NULL character must be set at the end.

#include<stdio.h>

void main()
{
char ch1[]={'p','r','o','\0'};
clrscr();
printf("%s",ch1);
getch();
 }

Subscribe via email

Enter your email address:

Delivered by FeedBurner