Home / C / Character array c / character array java

Character array c / character array java

Character array in c and character array in java can be implemented in two ways:
 
1. Character array using pointers: 
Using pointer you can assign a string value to a character array however it will still work as an character array not as a string. The pointer will point to the first index of your assigned string.
char *arr=”hello”;
     or
char *arr;
arr=”hello”
printf(“%c”, arr);
printf(“\n%c”,arr+3);

OUTPUT: h l

You can also use it for string array:-

printf("%s",arr);
printf("%s",arr+1);
printf("%s",arr+2);
 
OUTPUT: hello ello llo
2. Character array using Default Datatypes:-
Character array in Java:-
We have a String datatype so no problem at all. You can just implement the array using string datatype in array. 

String a[]={“character”,”array”,”in”,”java”}; for(i=0;i<5;i++) { System.out.println(a[i]); }

OUTPUT:

character array in java

Character array in C:- It is almost similar to that in Java but there we have to use char datatype.

char a[4][10]={“character”,”array”,”in”,”c”}; for(i=0;i<4;i++) { printf(“%s\n “,a[i]); }

OUTPUT: character array in c

Tags: Character array in c, Character array in java.

Check Also

program for display lower triangular matrix in java

What is Lower Triangular Matrix? Lower Triangular Matrix is if all the elements above the main ...

One comment

  1. I think other website proprietors should take this site as an example, very clean and great user genial design and style.

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.