Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Chapter 9

Character Arrays and Strings


Introduction

• A string is a sequence of characters terminated by a null character ‘\0’.


• ‘\0’ is automatically encountered at the end of the string.

Declaration of string
char ch[6] ;
Here, ch is a string which can store a maximum of 5 characters and 1 character is reserved for
‘\0’.
Initialization of string

char ch[6] = “HELLO” ; ch[0] ch[1] ch[2] ch[3] ch[4] ch[5]


‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’

char ch[6] = “Good” ; ch[0] ch[1] ch[2] ch[3] ch[4] ch[5]


‘G’ ‘o’ ‘o’ ‘d’ ‘\0’ ‘\0’

char ch[6] = { ‘P’, ‘r’, ‘e’, ‘m’, ‘\0’ } ; ch[0] ch[1] ch[2] ch[3] ch[4] ch[5]
‘P’ ‘r’ ‘e’ ‘m’ ‘\0’ ‘\0’

char ch[6] = “Program” ; ch[0] ch[1] ch[2] ch[3] ch[4] ch[5]


‘P’ ‘r’ ‘o’ ‘g’ ‘r’ ‘a’
Initialization of string

Invalid Initialization
char str3[5];
str3 = “GOOD”;
I/O operations on strings

scanf() printf()
char address[10] ;
char address[10] ;
scanf(“%s”, address);
scanf(“%s”, address); printf(“%s”, address);

gets() puts()
char address[10] ;
char address[10] ;
gets(address);
gets(address); puts(address);

Reading Text till ~ is encountered


char line[10] ;
scanf("%[^~]", line);
I/O operations on strings

#include<stdio.h>
void main()
{
char line[10] ;
printf("Enter string\n");
gets(line);
printf("The string is: ");
puts(line);
}
I/O operations on strings

#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
char a[10]="GLOBAL" ; char a[10]="GLOBAL" ;
int i,j; int i,j;
for(i=0;i<6;i++) for(i=0;i<6;i++)
{ {
for(j=0;j<=i;j++) for(j=0;j<=i;j++)
printf("%c\t", a[j]); putchar(a[j]);
printf("\n"); printf("\n");
} }
} }
Arithmetic Operations on Characters

To write a character in its integer representation, we may write it as an integer.


char x = ‘a’;
printf(“%d\n”, x); // 97
x = ‘z’–1;
printf(“%d\n”, x); // 122-1 = 121

char number[21] = “1988”;


int year = atoi(number);

The function atoi() converts the string “1988” (contained in number) to its numeric equivalent 1988
and assigns it to the integer variable year.
String conversion functions are stored in the header file <std.lib.h>.
String handling Functions

Function Action
#include<string.h>

strlen() finds the length of a string excluding ‘\0’

strcpy() copies one string over another

strcmp() compares two strings

strcat() concatenates two strings

strlwr() Converts the string to lowercase letters

strupr() Converts the string to uppercase letters


String handling Functions

strlen() #include<string.h>
void main()
strcpy() {
char a[21] ;
strcmp()
int len;
strcat() printf("Enter a string\n");
gets(a);
strlwr() len=strlen(a);
printf("The length of the string is: %d", len);
strupr() }
String handling Functions

works similar to string-assignment operator.


Syntax:
strlen() strcpy(string1, string2);
strcpy()
Assigns the contents of string2 to string1.
strcmp() string2 may be a string variable or a string constant.
Example:
strcat() strcpy(city, “DELHI”);
will assign the string “DELHI” to the string variable city.
strlwr()

strupr() strcpy(city1, city2);


will assign the contents of the string variable city2 to the string variable city1.
The size of the array city1 should be large enough to receive the contents of
city2.
String handling Functions

strlen() #include<stdio.h>
#include<string.h>
strcpy() void main()
{
strcmp()
char a[21],b[21] ;
strcat() printf("Enter a string\n");
gets(a);
strlwr() strcpy(b,a);
printf("The copied string is: %s",b);
strupr() }
String handling Functions

Syntax:
strlen() strcmp(string1, string2);
It compares string1 with string2 (case sensitive)
strcpy()
Returns 0 if string1==string2
strcmp()
Returns value > 0 if string1 > string2
strcat() Returns value < 0 if string1 < string2

strlwr() Examples:
char name1[21]=“string”, name2[21]=“string”;
strupr() strcmp(name1, name2);
strcmp(name1, “John”);
strcmp(“Rom”, “Ram”);
String handling Functions

#include<stdio.h>
strlen()
#include<string.h>
strcpy() void main()
{
strcmp() char name1[21]="string", name2[21]="string";
int p1, p2, p3, p4;
strcat() p1=strcmp(name1, name2);
p2=strcmp(name1, "String");
strlwr()
p3=strcmp(name1, "Lohit");
strupr() p4=strcmp("RAM", "ROM");
printf("p1=%d\np2=%d\np3=%d\np4=%d\n",p1,p2,p3,p4);
}
String handling Functions

char name1[21]="string", name2[21]="string"; name1 0 1 2 3 4 5 6 - - 20


int p1, p2, p3, p4; - -
p1=strcmp(name1, name2); s t r i n g \0
0 1 2 3 4 5 6 - - 20
p2=strcmp(name1, "String"); name2 - -
p3=strcmp(name1, "Lohit");
s t r i n g \0
p4=strcmp("RAM", "ROM"); 0 1 2 3 4 5 6 - - 20
- -
S t r i n g \0
0 1 2 3 4 5
L o h i t \0

0 1 2 3
R A M \0

0 1 2 3
R O M \0
String handling Functions

strlen() Joins two strings together.

strcpy() Syntax:
strcat(string1, string2);
strcmp()
• string2 is appended to string1.
strcat()
• It does so by removing the null character at the end of string1 and placing
strlwr() string2 from there.
strupr() • The string at string2 remains unchanged.
• We must make sure that the size of string1 (to which string2 is appended) is
large enough to accommodate the final string.
String handling Functions

strlen()

strcpy()

strcmp()
strcat(part1, part2);
strcat()

strlwr()

strupr()
String handling Functions

#include<stdio.h>
strlen() #include<string.h>
void main()
strcpy() {
char name1[10]="abc", name2[10]="xyz";
strcmp()
printf("Before concatenation\n");
strcat() printf("name1= %s\n",name1);
printf("name2= %s\n",name2);
strlwr() strcat(name1, name2);

strupr() printf("After concatenation\n");


printf("name1= %s\n",name1);
printf("name2= %s\n",name2);
}
String handling Functions

Used to convert a given string into lowercase.


strlen()
Syntax:
strcpy() strlwr(string);

strcmp() #include<stdio.h>
#include<string.h>
strcat() void main()
{
strlwr()
char a[21]="C PROGRAM";
strupr() printf("Original string= %s\n",a);
strlwr(a);
printf("Converted string= %s\n",a);
}
String handling Functions

Used to convert a given string into uppercase.


strlen()
Syntax:
strcpy() strupr(string);

strcmp() #include<stdio.h>
#include<string.h>
strcat() void main()
{
strlwr()
char a[21]="good morning";
strupr() printf("Original string= %s\n",a);
strupr(a);
printf("Converted string= %s\n",a);
}
Programs on strings

#include<stdio.h>
#include<string.h>
void main()
1. Write a C program, which reads your name {
from the keyboard and outputs a list of char name[21] ;
ASCII codes, which represent your name. int i,len;
printf("Enter your name:\n");
gets(name);
len=strlen(name);
printf("The name is ASCII form:\n");
for(i=0;i<len;i++)
printf("%d",name[i]);
}
Programs on strings

#include<stdio.h>
void main()
{
char a[21] ;
2. Write a C program to find
int len=0, i;
length of a string without using
printf("Enter a string\n");
library function. gets(a);
for(i=0;a[i]!='\0';i++)
len++;

printf("The length of the string is: %d", len);


}
Programs on strings

3. Write a C program to input a string, convert lowercase letters to uppercase and vice
versa without using library functions.

#include <stdio.h> for(i=0;i<len;i++)


#include<string.h> {
void main() if(a[i]>='A' && a[i]<='Z')
{ a[i]=a[i]+32;
char a[21]; else if(a[i]>='a' && a[i]<='z')
int i, len; a[i]=a[i]-32;
printf("Enter a string\n"); }
gets(a); printf("The modified string is %s", a);
len=strlen(a); }
Programs on strings

4. Write a C program to find total number of alphabets, digits in a string without using library
functions.

#include <stdio.h> for(i=0;i<len;i++)


#include<string.h> {
void main() if((a[i]>='A' && a[i]<='Z') || (a[i]>='a' && a[i]<='z'))
{ alpha++;
char a[21]; else if(a[i]>='0' && a[i]<='9')
int i, len, alpha=0, digit=0; digit++;
printf("Enter a string\n"); }
gets(a); printf("No. of alphabets=%d\nNo. of digits=%d", alpha, digit);
len=strlen(a); }
Programs on strings

5. Write a C program to count total number of vowels and consonants in a string.

#include<stdio.h> for(i=0;i<len;i++)
#include<string.h> {
void main() if (isalpha(a[i]))
{ {
char a[21]; if(a[i]=='A' || a[i]=='E' || a[i]=='I' || a[i]== 'O' || a[i]=='U')
int i, len, vow=0, cons=0; vow++;
printf("Enter a string\n"); else cons++;
gets(a); }
strupr(a); }
len=strlen(a); printf("No. of vowels = %d\n", vow);
printf("No. of consonants = %d\n", cons);
}

You might also like