Character Array and Strings
Character Array and Strings
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
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.
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
#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
1. using printf()
#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;
return 0;
}
Write a program to check whether a
string is palindrome or not
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