0% found this document useful (0 votes)
0 views5 pages

PR 6

This document outlines an experiment focused on understanding character and string arrays in C programming. It covers the declaration, initialization, and reading of strings, as well as various string functions and their purposes. Additionally, it includes example code snippets and exercises for further practice.

Uploaded by

Pdf Web
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

PR 6

This document outlines an experiment focused on understanding character and string arrays in C programming. It covers the declaration, initialization, and reading of strings, as well as various string functions and their purposes. Additionally, it includes example code snippets and exercises for further practice.

Uploaded by

Pdf Web
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPERIMENT NO.

06

DATE OF PERFORMANCE: GRADE:

DATE OF ASSESSMENT: SIGNATURE OF LECTURER/ TTA:

AIM: To Understand the Character and String Array Using C Programs

THEORY:
In C programming, array of character are called strings. A string is terminated by null
character ‘/0’. For example,

"c string tutorial"

Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null
character at the end of string.

FIGURE 6.1 STRING

DECLARATION OF STRINGS
Strings are declared in C in similar manner as arrays. Only difference is that, strings are of
char type.

char s[5];

FIGURE 6.2 DECLARATION

Strings can also be declared using pointer.

char *p

INITIALIZATION OF STRINGS

In C, string can be initialized in different number of ways.

Page 1 of 5
char c[]="abcd";

OR,

char c[5]="abcd";

OR,

char c[]={'a','b','c','d','\0'};

OR;

char c[5]={'a','b','c','d','\0'};

FIGURE 6.3 INITIALISATION


String can also be initialized using pointers,

char *c="abcd";

READING STRINGS FROM USER


READING WORDS FROM USER
char c[20];
scanf("%s",c);

String variable c can only take a word. It is beacause when white space is encountered, the
scanf() function terminates.

EXAMPLE:

#include <stdio.h>

void main(){

char name[20];

printf("Enter name: ");

scanf("%s",name);

printf("Your name is %s.",name);


Page 2 of 5
getch();

OUTPUT:

Enter name: Dennis Ritchie

Your name is Dennis.

Here, program will ignore Ritchie because, scanf() function takes only string before the
white space.

READING A LINE OF TEXT


There are predefined functions gets() andputs() in C language to read and display string
respectively.

void main(){

char name[30];

printf("Enter name: ");

gets(name); //Function to read string from user.

printf("Name: ");

puts(name); //Function to display string.

getch();

OUTPUT
Enter name: Tom Hanks
Name: Tom Hanks

STRING FUNCTIONS:
S.N. Function & Purpose

1 strcpy(s1, s2);

Copies string s2 into string s1.

2 strcat(s1, s2);

Page 3 of 5
Concatenates string s2 onto the end of string s1.

3 strlen(s1);

Returns the length of string s1.

4 strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater


than 0 if s1>s2.

5 strchr(s1, ch);

Returns a pointer to the first occurrence of character ch in string s1.

6 strstr(s1, s2);

Returns a pointer to the first occurrence of string s2 in string s1.

EXAMPLE:

#include <stdio.h>

#include <string.h>

void main () {

char str1[12] = "Hello";

char str2[12] = "World";

char str3[12];

int len ;

/* copy str1 into str3 */

strcpy(str3, str1);

printf("strcpy( str3, str1) : %s\n", str3 );

/* concatenates str1 and str2 */

Page 4 of 5
strcat( str1, str2);

printf("strcat( str1, str2): %s\n", str1 );

/* total lenghth of str1 after concatenation */

len = strlen(str1);

printf("strlen(str1) : %d\n", len );

getch();

OUTPUT:

strcpy( str3, str1) : Hello

strcat( str1, str2): HelloWorld

strlen(str1) : 10

EXERCISE:

1. Write a program to count the number of ‘A’ in the string entered by user.
2. Write a programto reverses the string.
3. Write a program to check whether entered string is palindrome or not.
4. Write a program to convert lower case string to upper case.
5. Write a program to check whether entered two strings are same or not.

Page 5 of 5

You might also like