Character Arrays and Strings
Character Arrays and Strings
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] = { ‘P’, ‘r’, ‘e’, ‘m’, ‘\0’ } ; ch[0] ch[1] ch[2] ch[3] ch[4] ch[5]
‘P’ ‘r’ ‘e’ ‘m’ ‘\0’ ‘\0’
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);
#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
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() #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
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
0 1 2 3
R A M \0
0 1 2 3
R O M \0
String handling Functions
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);
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
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++;
3. Write a C program to input a string, convert lowercase letters to uppercase and vice
versa without using library functions.
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 (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);
}