UNIT4 String
UNIT4 String
String
you do not place the null character at the end of a string constant. The C
compiler automatically places the '\0' at the end of the string when it
initializes the array
Memory presentation
There are two ways to declare a string in c language.
By char array
By string literal
string by char array
char c[6]={'a','p','p','l','e','\0'};
char c[]={'a','p','p','l','e','\0'};
string by the string literal
char c1[]="apple";
• char
chara[]={‘a’,’p’,’p’,’l’,’e’,’\0’};
a [] ={‘a’.’p’,’p’,’l’,’e’,’\0’};
• char
charb[]=“apple”;
b [] =“apple”;
Reading and Printing a String
Reading and Printing a String
scanf ()
• not used the ‘&’ sign with string
#include <stdio.h> name ‘s’ in scanf statement. We
main() know that the ‘&’ sign is used to
{ provide the address of the variable
char s[10]; to the scanf() function to store the
printf("Enter a string : "); value read in memory.
scanf("%s",s); • As s[] is a character array so using s
printf("\nThe entered string is\n"); without braces ‘[‘ and ‘]’ will give
printf("%s",s); the base address of this string.
} That’s why not used ‘&’ in as we are
already providing the base address
of the string to scanf.
Reading and printing a String [with embedded
spaces]
#include <stdio.h> gets
main()
{ • It is used to read input from the
char s[10];
standard input(keyboard).
printf("Enter a string : ");
gets(s); • It is used to read the input until it
printf("\nThe entered string is\n"); encounters newline or End Of
printf("%s",s); File(EOF).
} • scanf can read multiple values of
different data types whereas gets()
will only get character string data.
How to read entire string using scanf()
#include <stdio.h>
#include<stdio.h>
int main ()
{
char s[] = “brindha";
int i = 0;
int count = 0;
while(i<=7)
{
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{
count ++;
}
i++;
}
printf("The number of vowels %d",count);
}
Using the null character
#include<stdio.h>
int main ()
{
char s[] = “KARUNYA";
int i = 0;
int count = 0;
while(s[i] != NULL)
{
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{
count ++;
}
i++;
}
printf("The number of vowels %d",count);
}
String Functions
String Functions
Function Name Functionality Syntax Example
strlen() To find the length of a int_var = strlen(s1); strlen(“apple”) => 5
string
strupr() Converts a string to strupr(s1); strupr(“apple”) => APPLE
uppercase
strrev() Reverses a string strrev(s1) strrev(“live”) => evil
strlwr() Converts a string to strlwr(s1); strlwr(“APPLE”) => apple
lowercase
strcpy() To copy or assign a string strcpy(s1, s2) strcpy(s1, “pigeon”) => pigeon is
to another variable assigned to s1
strcmp() To compare a string with strcmp(s1, s2) strcmp(“dove”, “dove”) => 0 =>
another strings are equal
strcat() To concatenate or join two int_var=strcat(s1, s2) strcat(“butter”,”flies”) =>
strings butterflies
String Length – Number of characters in the string
}
String Copy
main()
{
char a[]=“pigeon”, b[20];
strcpy(b,a);
printf("\nCopied string :
%s",b);
}
String Compare
#include <stdio.h>
#include <string.h>
Output
Strings are not equal
main()
{
char a[]=“abc”, b[]=“acc”;
if (strcmp(a,b)==0)
printf("\nStrings are equal");
else
printf("\nStrings are not equal");
}
String Concatenation
#include<stdio.h> for(i=0;i<=1;i++){
int main() if(islower(s[i])){
{ s[i]=toupper(s[i]);
char s[20]; }
int l,i; }
printf(“Enter string”); printf(“the converted string is:%s”,s);
gets(s); }
l=strlen(s);
count vowels in a sentence
#include<stdio.h> for(i=0;i<l;i++){
int main(){ for(j=0;j<13;j++){
int i,j,l=0,vowel=0; if(s[i]==v[j]){
char V[13]=”AaEeIiOoUu”,s[100]; vowel++;
printf(“enter sentence”); break;
gets(s); }
l=strlen(s); }
printf(“%d”,vowel);
}
}