0% found this document useful (0 votes)
13 views

Character Array and Strings

Uploaded by

Joel George
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Character Array and Strings

Uploaded by

Joel George
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

CHARACTER ARRAY

AND STRINGS
STRING
• C does not support string datatype
• It allows to declare string as character
array
• The general form of declaration is;-
char string_name [size];
• Character array aways ends with a null
character or terminating character ‘\0’
STRING
• While the compiler assigns a character
string to a character array , it
automaticaaly supplies null character(‘\0’)
at the end of string
INITIALIZATION OF CHARACTER ARRAY

• Character arrays may be initialized at the time of


declaration
• C program allows to declare character array in
either of forms
char city[9]=”NEW YORK”;
char city[9]={‘N’,’E’,’W’,’ ‘,’Y’,’O’,’R’,’K’,’\0’};
(blank space and null character also counted in
size of array)
INITIALIZATION OF CHARACTER ARRAY

• We can also declare the size much larger


than the string size
• eg;- char str[10]=”GOOD”

G O O D \0 \0 \0 \0 \0 \0
READING OF STRING
1. Using scanf()
• The scanf() can be used to read a string with %s format
specification.
• The problem with the scanf() is that it terminates its input
on the first white space(blank space, tabs, carriage
return, new lines) it finds.

eg;-if the following line of text is is typed using scanf() ie


NEW YORK, only the string NEW will be read into the
array address

• The scanf() automatically terminates the string that is


read with a null character
• In the case of character array the
ampersand(&) is not required before the
variable name

eg;- char address[10];


scanf(“%s”, address);
Reading using scanf()
#include <stdio.h>
#include <stdlib.h>

int main()
{
char name[30];
printf("enter the name\n");
scanf("%s", name);
printf("Display the name\n");
printf("%s", name);
return 0;

}
2. Reading string using getchar and gets function

• C getchar is a standard library function that takes a


single input character from standard input.

• It is defined inside the <stdio.h> header file.


Implementing getchar()

#include <stdio.h>
int main()
{
char ch;
ch= getchar();//reading
character

printf("The entered
character is : %c", ch);
return 0;
}
gets()
• For reading a string with spaces, we can use gets() in C
programming language
• It reads the characters from keyboard until a new line
character is encounterd and then appends a null
character to string.
eg-
• If I use the gets() function, I could input the full name
John Arthur

• If I use the scanf(), I could input only John


Implementing gets()
#include <stdio.h> OUTPUT
#include <stdlib.h> enter the line
My name is Neha Manoj
My name is Neha Manoj
int main() Process returned 0 (0x0)
{ execution time : 13.372 s
char line[20]; Press any key to continue.

printf("enter the line\n");


gets(line);// reading line
printf("%s", line);
return 0;
}
Writing strings to screen

1. using printf()

The format specification %s can be used to display an array of


characters that is terminated by null character

eg- printf(“%s”, name);


2. Using putchar

using putchar()-to display a


single character on screen

#include<stdio.h>
int main()
{
char ch ='A';
putchar(ch);//printing character
return 0;
}
Using puts()

#include<stdio.h>
int main()
{
char line[50];
printf("Enter the line\n");
gets(line);//Reading string
printf("Display the entered
line\n");
puts(line);//Display string
return 0;
}
STRING HANDLING FUNCTIONS INC
#include<stdio.h>
#include<string.h>

int main()
{
char S1[50] ="EXAMPLE
FOR STRCPY";
char S2[50];
strcpy(S2,S1);
printf("%s", S2);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{ Output
char S1[] ="My name is"; My name is John
char S2[]="John Arthur"; Arthur
strcat(S1,S2);
printf("%s", S1);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char S1[] ="cat";
char S2[]="dog";
int S3;
S3= strcmp(S1,S2)
printf("%d",S3);
return 0;
}
#include<stdio.h>
#include<string.h>

int main()
{
char S1[] ="John Arthur";
int size;
size= strlen(S1);

printf("%d",size);

return 0;
}
Write a program to find length of string
without using strlen
#include<stdio.h>
printf(“Length of string is %d”,
int main() length);
{
int length, c=0; return 0;
}
char s[50];
printf(“Enter the string\n”);
gets(s);
while(s[c]!=‘\0’)
{
length++;
c++;
}
W.A.P TO COUNT THE NUMBER OF VOWELS IN A
GIVEN STRING
#include <stdio.h>
int main()
{
int c = 0, count = 0;
char s[1000];
printf("Input a string\n");
gets(s);
while (s[c] != '\0')
{
if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] == 'E' || s[c] == 'i' || s[c] == '
I' || s[c] =='o' || s[c]=='O' || s[c] == 'u' || s[c] == 'U’)
count++;
c++;
}
printf("Number of vowels in the string: %d", count);
return 0;
}
TO CONVERT STRING TO LOWERCASE
TO CONVERT STRING TO UPPERCASE
W. A .P TO CONVERT THE CASE OF STRING WITHOUT
USING STRING HANDLING FUNCTIONS

• ASCII value of A to Z is 65 to 90
• ASCII value of a to z is 97 to 129

#include <stdio.h>
int main()
{
char s[1000];
int c;

printf("Enter a string \n");


gets(s);// Reading the string
while( s[c]!=‘\0’)
{
if (s[c] >= 'a' && s[c] <= 'z’)
{
s[c] = s[c] - 32;
}
else if (s[c] >= ’A' && s[c] <= ’Z’)
{
s[c] = s[c] + 32;
}
c++;
}
puts(s);

return 0;
}
Write a program to check whether a
string is palindrome or not

• A palindrome is a string, which is the same


when read in both forward and backward
ways.
eg-
Index= 0 1 2 3 4

R A D A R

S[0]==s[4]
S[1]==s[3]
#include <stdio.h>
#include <string.h> for(i=0;i< length/2;i++)
int main() if (s[i]!=s[lenth-i-1)
{ flag=1;
char s[20]; break;
int i, length; if (flag= = 0)
int flag = 0; {
printf("Enter a string\n "); printf("%s palindrome\n", s);
}
scanf("%s", s); else { printf("%s is not a
palindrome\n", s);
// Calculate the string length }
return 0;
length = strlen(s); }
PROGRAM TO CONCANTENATE TWO STRINGS for(i=len,j=0; str2[j]!='$';i++,j++)
WITHOUT STRING HANDLING FUNCTIONS
#include <stdio.h> #include <stdlib.h> main() { {
char str1[50],str2[50]; int i=0, j, len=0; str1[i]=str2[j];
printf("Enter string1 enter with$ symbol\n"); }
gets(str1); printf("Enter string2 enter with$
symbol\n"); gets(str2); str1[i]='\0';
while(str1[i]!='$') printf(“After concantenation\n”):
{ puts(str1);
len++; }
i++;
} OUTPUT
str1[i]=' ';
len++; Enter string1 enter with$ symbol
str1[len]='\0'; Read$
Enter string2 enter with$ symbol
C Programming subject$
After concantenation
Read C Programming subject

Process returned 0 (0x0) execution


time : 15.997 s
Press any key to continue.

You might also like