[go: up one dir, main page]

0% found this document useful (0 votes)
27 views6 pages

Lecture 20-21-22 - Strings or Character Arrays in C

Uploaded by

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

Lecture 20-21-22 - Strings or Character Arrays in C

Uploaded by

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

STRINGS (AN ARRAY OF CHARACTERS) IN C

TABLE OF CONTENTS
=======================

S TRINGS (AN ARRAY OF CHARACTERS) IN C ................................................................................................................................ 1


S TRINGS / A CHARACTER ARRAY IN C: ......................................................................................................................................... 2
DECLARING AND INITIALIZING A S TRING ...................................................................................................................................... 2
S TRING INPUT AND OUTPUT ............................................................................................................................................................ 2
PROGRAMS TO PERFORM VARIOUS OPERATIONS ON S TRINGS WITHOUT U SING STRING HANDLING FUNCTIONS .................. 4
1. PROGRAM TO FIND LENGTH (NO OF CHARACTERS ) OF A STRING ................................................................................... 4
2. PROGRAM TO CONCATENATE T WO S TRINGS .................................................................................................................. 5
3. PROGRAM TO COMPARE TWO S TRINGS .......................................................................................................................... 6

1|Page
STRINGS / A CHARACTER ARRAY IN C:
String is a sequence of characters that is treated as a single data item and terminated by null
character '\0'. Remember that C language does not support strings as a data type. A string is actually
one-dimensional array of characters in C language. These are often used to create meaningful and
readable programs.

For example: The string "hello world" contains 12 characters including '\0' character which is
automatically added by the compiler at the end of the string.

DECLARING AND INITIALIZING A STRING


As string in C is an array of characters, therefore, strings are declared the same way an array of integers
is declared. The general syntax of declaring a string is given as:

char array_name [size];

Example: char name [15]; // Declares name a string / character array for 14 characters

// and keeps one space reserve for ‘\0’ character

There are different ways to initialize a character array variable.

 char name[13] = "Zahidhussain"; // valid character array initialization / string initialization


 char name[10] = {'L','e','s','s','o','n','s','\0'}; // valid initialization

Remember that when we initialize a character array by listing all of its characters separately then we
must supply the '\0' character explicitly.

Some examples of illegal initialization of character array are

 char ch[3] = "hell"; // Illegal

 char str[5];
str = "hell"; // Illegal as we can’t put declaration and initialization separately

STRINGINPUT AND OUTPUT


Input function scanf() can be used with %s format specifier to read a string input from the terminal.
But there is one problem with scanf() function, it terminates its input on the first white spaceit
encounters. Therefore if you try to read an input string "Hello World" using scanf() function, it will
only read Hello and terminate after encountering white spaces.

However, C supports a format specification known as the edit set conversion code %[..] that can be
used to read a line containing a variety of characters, including white spaces.

2|Page
Example:
#include<stdio.h>

#include<string.h>

voidmain()

charstr[20];

printf("Enter a string");

scanf("%[^\n]", &str); //scanning the whole string, including the white spaces

printf("%s", str);

Another method to read character string with white spaces from terminal is by using the
gets() function.e.g
char text[20];

gets(text); // gets the input(no of characters) from the user

printf("%s", text); //displays the inputted no of characters on the output screen

Same way we can use puts () function instead of printf to print the results. e.g
char text[20];

gets(text); // gets the input(no of characters) from the user

puts(text); //displays the inputted no of characters on the output screen

3|Page
PROGRAMS TO PERFORM VARIOUS OPERATIONS ON STRINGS WITHOUT
USING STRING HANDLING FUNCTIONS

1. PROGRAM TO FIND LENGTH (NO OF CHARACTERS ) OF A STRING


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
int i, count=0;
printf("Enter any string\n");
gets (name);
printf (‚You have entered the following string\n‛);
puts (name);
//Logic for finding the Length of the string
i=0;
while(name[i]!= ‘\0’)
{
count = count + 1;
i++;
}
printf(‚The length of above given string is %d characters \n‛, count);
getch();
}

OUTPUT:
Enter any string
Zahid Hussain
You have entered the following string
Zahid Hussain
The length of string is 13 characters

4|Page
2. PROGRAM TO CONCATENATE TWO STRINGS
#include<stdio.h>
#include<conio.h>
void main()
{
Char name1[20],name2[20];
int i, count=0;
printf("Enter first string\n");
gets (name1);
printf("Enter second string\n");
gets (name2);
printf (‚You have entered the following first string\n‛);
puts (name1);
printf (‚You have entered the following second string\n‛);
puts (name2);
i=0;
while(name1[i]!= ‘\0’)
{
count = count + 1;
i++;
}
name1[count]=’ ’;
count=count+1;
i=0;
while(name2[i]!= ‘\0’)
{
name1[count]=name2[i];
i++;
count++;
}
name1[count]= ‘\0’
// The second string has been appended to the first string but itself remained as it
printf(‚The first string will be now \n ‛);
puts (name1);
printf(‚The second is like as it was and is given as \n ‛);
puts (name2);
getch();
}
OUTPUT:
Enter first string
Zahid
Enter second string
Hussain
You have entered the following first string
Zahid
You have entered the following second string
Hussain
The first string will be now
Zahid hussain
The second is like as it was and is given as
hussain

5|Page
3. PROGRAM TO COMPARE TWO STRINGS
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[20],name2[20];
int i, flag = 0;
printf("Enter first string\n");
gets (name1);
printf("Enter second string\n");
gets (name2);
printf (‚You have entered the following first string\n‛);
puts (name1);
printf (‚You have entered the following second string\n‛);
puts (name2);
i=0;
while(name1[i]!= ‘\0’ || name2[i]!= ‘\0’)
{
if (name1[i] != name2 [i])
{
flag = 1;
break;
}
i++;
}
if(flag == 0)
{
printf (‚The strings are Identical‛);
}
else
{
printf (‚The strings are NOT Identical‛);
}
OUTPUT 1:
Enter first string
Zahid
Enter second string
zahid
You have entered the following first string
Zahid
You have entered the following second string
zahid
The strings are Identical
OUTPUT 2:
Enter first string
Zahid
Enter second string
Wani
You have entered the following first string
Zahid
You have entered the following second string
Wani
The strings are NOT Identical

6|Page

You might also like