Character Array in C

 Character Array in C

In C language String is a 1-dimension array of characters. In C we don’t have string data type to deal with strings. We can deal with string by using character array. String is sequence of characters. This sequence of characters are accessed and stored in a variable with character array. This string is terminated with null character (\0 back slash zero).

Syntax of character declaration:

char variable_name[size]={list of strings};

char is character data type

variable_name is the character array to store string

substrict[] is used to define length of string

Example,

char str1[6]={‘H’,’l’,’l’,’o’,’\0’};

Here str1 is character array which store Hello string. The string is terminated with null character\0.  If we donot use null character to terminated string compiler will automatically take null character to terminated string.

Another example

char str2[]={‘h’,’e’,’l’,’l’,’o’,’\0’};

char str3=”Hello”

C program to initialize string and display on screen

#include<stdio.h>

#include<conio.h>

void main()

{

char str1[6]={‘H’,’e’,’l’,’l’,’o’,’\0’};

clrscr();

printf(“The string is:%s”,str1);

return 0;

}

Output:

The string is: Hello


C program to initialize string

#include<stdio.h>

#include<conio.h>

void main()

{

char str2[6]=”Hello”;

clrscr();

printf(“The string is:%s”,str2);

return 0;

}

Output: 

The string is: Hello


C program to take string at run time through terminal

#include<stdio.h>

#include<conio.h>

void main()

{

char str2[6];

clrscr();

printf(“Enter string\n”);

scanf(“%s”,&str2);

printf(“The string is:%s”,str2);

return 0;

}

Output:

Enter string

Manisha

The string is Manisha


Working with multiple strings

The multiple strings in C language are accessed and processed with 2 dimensions array. 

For example,


#include<stdio.h>

#include<conio.h>

void main()

{

Char name[3][20]={“Radha”, “Shyam”, “RamKrishna”};

printf(“The names are : \n”);

for(i=0;i<3;i++)

{

printf(“%s\t”,name[i]);

}

}

Output:

The name is Radha Shyam RamKrishna

In above example we have stored 3 strings i.e. three rows and 20 columns i.e. 20 indices is created to stored max 20 charactered of 3 strings. 


C Program to take strings at run time through terminal

#include<stdio.h>

#include<conio.h>

void main()

{

Char name[3][20];

Printf(“Enter Strings\n”);

For(i=0;i<3;i++)

{

scanf(“%s”,&name[i]);

}

printf(“The names are : \n”);

for(i=0;i<3;i++)

{

printf(“%s\t”,name[i]);

}

}

Output:

Enter strings

Radha Seeta Geeta

The names are:

Radha Seeta Geeta



Use of gets() and puts() function:

The gets() and puts() functions are builtin or library functions which are used to read and display string on screen. These are input and output functions in C language. To support both the functions stdio.h header file is used. The gets() function allows the user to enter the space separated string. It return the string entered by user.

Declaration,

char s[20];

gets(s);

Reading the string using get() function

#include<stdio.h>

#include<conio.h>

void main()

{

char s[20];

clrscr();

printf(“Enter string\n”);

gets(s);

printf(“The string is: %s”,s);

}

Output:

Enter string

Hello

The string is: Hello



C puts() function:

The put() function is very much similar to printf() function in C. putf() function is used to print or display string on screen.

C program to enter and display string with gets() and puts() function

#include<stdio.h>

#include<conio.h>

void main()

{

Char name[20];

printf(“Enter string\n”);

gets(name);

printf(“The string is\n”);

puts(name);

}

Output:

Enter string

Hello

The string is

Hello


String Functions in C:

The string functions are used to perform various operations on string.

To support string functions, we need to include string.h header file in program

Following are some string functions in C,

  1. Strlen(): 

It is String length function. It returns the length of given string.

Syntax:

strlen(string_name);


For example,

#include<stdio.h>

#include<string.h>

void main()

{

char str1[20]={‘h’,’e’,’l’,’l’,’o’,’\0’};

printf(“The length of string is : %d”,strlen(str1));

}

Output:

The length of string is: 5



  1. strcpy(): 

The strcpy is string copy function is used to copy the string. It helps to copies the source string to destination string.

Syntax:

strcpy(destination, source);

For example

#include<stdio.h>

#include<string.h>

void main()

{

char str1[20]={‘h’,’e’,’l’,’l’,’o’,’\0’};

char str2[20];

strcpy(str2,str1);

printf(“The copied string is : %s”,str2);

}

Output:

The copied string is: hello


  1. strcat() function:

The strcpy() function is string concatenation function which is used to combine two strings.

Syntax:

strcat(first_string,second_string);


For example,

#include<stdio.h>

#include<string.h>

void main()

{

char str1[10]={‘h’,’e’,’l’,’\0’};

char str2[10]={‘l’,’o’,’\0’};

strcat(str1,str2);

printf(“The combined string is %s”,str1);

}

Output:

The combined string is hello


  1. strcmp() function:

strcmp() function is used to compare two strings. It returns 0 if both the strings are true. First string is compared with second string.

Syntax:

strcmp(first_string, second_string);

For example:

#include<stdio.h>

#include<string.h>

void main()

{

char str1[10];

char str2[10];

printf(“Enter first string\n”);

gets(str1);

printf(“Enter second string\n”);

gets(str2);

if(strcmp(str1==str2)

{

Printf(“Strings sre same”);

}

else

{

Printf(“Strings are not same”);

}

}

Output:

Enter first string

Hello

Enter second string

Hello

Strings are same



  1. strrev function():

strrev() function is used to reverse given string.

Syntax:

Strrev(string);


For example,

#include<stdio.h>

#include<string.h>

void main()

{

char str1[10];

printf(“Enter  string\n”);

gets(str1);

printf(“The string is :%s\n”,str1);

printf(“The reversed string is : %s”,strrev(str1));


Output:

Enter string

Hello

The string is : 

Hello

The reversed string is : olleh


  1. strlwr() function:

strlwr() function returns string or characters into lower case.

Syntax:

Strlwr(string);


For example,

#include<stdio.h>

#include<string.h>

void main()

{

char str1[10];

printf(“Enter  string\n”);

gets(str1);

printf(“The string is :%s\n”,str1);

printf(“The lowercase string is: %s”, strlwr(str1));

}

Output:

Enter string

HELLO

The string is: 

HELLO

The lowercase string is: hello



  1. strupr() function:

strupr() function is used to convert string into upper case.

Syntax,

Strupr(string);


For example,

#include<stdio.h>

#include<string.h>

void main()

{

char str1[10];

printf(“Enter  string\n”);

gets(str1);

printf(“The string is :%s\n”,str1);

printf(“The uppercase string is: %s”,strupr(str1));

}

Output:

Enter string

Hello

The string is:

hello

The uppercase string is : HELLO










टिप्पणी पोस्ट करा

0 टिप्पण्या