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

UNIT4 String

A string is a sequence of characters stored as a character array terminated with a null character. There are two ways to declare a string in C - using a character array or a string literal. The key differences are that the null character must be manually added for character arrays but is automatically added for string literals, and character arrays can be reassigned but string literals cannot. Common string functions include strlen() to get the length, strcpy() to copy strings, strcmp() to compare strings, and strcat() to concatenate strings.

Uploaded by

meclinejosefl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

UNIT4 String

A string is a sequence of characters stored as a character array terminated with a null character. There are two ways to declare a string in C - using a character array or a string literal. The key differences are that the null character must be manually added for character arrays but is automatically added for string literals, and character arrays can be reassigned but string literals cannot. Common string functions include strlen() to get the length, strcpy() to copy strings, strcmp() to compare strings, and strcat() to concatenate strings.

Uploaded by

meclinejosefl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Strings

String

A string is a sequence of characters defined between


double quotation marks
Eg: printf (“WELL DONE”);
 Stored using character arrays
 Automatically terminated with a null character (\0)
 Eg. char a[]=“apple”;
char a[]={‘a’, ‘p’, ‘p’, ‘l’,‘e’,‘\0’}
String

 Declaring and initializing


Strings are declared as an array of characters.

The syntax is:


char string_name[size];

Eg: char city [10];


char name[30];
null character '\0'

 Strings are actually one-dimensional array of characters terminated by


a null character '\0'. Thus a null-terminated string contains the characters
that comprise the string followed by a null.
 The following declaration and initialization create a string consisting of the
word "Hello". To hold the null character at the end of the array, the size of the
character array containing the string is one more than the number of
characters in the word "Hello.“

 char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};


 char greeting[] = "Hello";

 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'};

 While declaring string, size is not mandatory. So we can write


the above code as given below:

 char c[]={'a','p','p','l','e','\0'};
string by the string literal

 char c1[]="apple";

 In such case, '\0' will be appended at the end of the string by


the compiler.
Difference between char array and string literal

There are two main differences between char array and


literal.
 We need to add the null character '\0' at the end of the
array by our self whereas, it is appended internally by the
compiler in the case of the string literal.
 The string literal cannot be reassigned to another set of
characters whereas, we can reassign the characters of the
array.
Initialization

• 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>

int main() %[^\n]


{ • It is an edit conversion code.
• The edit conversion code %[^\n]
char str[20]; can be used as an alternative of
printf("Enter something\n"); gets.
• This edit conversion code can be
// Here \n indicates that take the input
// until newline is encountered used to read a line containing
scanf("%[^\n]s", str); characters like variables and
printf("%s", str); even whitespaces using scanf().
return 0;
}
Traversing String

There are two ways to traverse a string.


 By using the length of string
 By using the null character.
Using the length of string

#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

#include <stdio.h>  Output


#include <string.h> The length of
the string = 6
main()
{
char a[] = “window”;
printf("\nThe length of the string %d\n",
strlen(a));
}
Uppercase

#include <stdio.h> Output:


#include <string.h> String in upper case :
SHALLOW
main()
{
char a[]=“shallow”;
printf("String in upper case :
%s",strupr(a));
}
Lower Case

#include <stdio.h> Output


#include <string.h> String in lower
case : sparrow
main()
{
char a[]=“SPARROW”;
printf("String in lower case : %s",strlwr(a));
}
String Reverse

#include <stdio.h>  Output


#include <string.h> The reversed
string is: stressed
main()
{
char a[]=“desserts”
printf("\nThe reversed string is = %s\
n",strrev(a));

}
String Copy

#include <stdio.h> Output:


#include <string.h> Copied string : pigeon

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> Output


#include <string.h> Concatenated string : SweetCorn
main()
{
char a[]=“Sweet”, b[]=“Corn”;
strcat(a,b);
printf("Concatenated string : %s",a);
}
#include <stdio.h> printf("\nLower case =
#include <string.h> %s",strlwr(c));
main() printf("\nConcatenated strings =
{ %s",strcat(a,b));
char a[]="pine";
char b[]="apple"; if (strcmp(b,c)==0)
char c[20]; printf("\nStrings are identical");
printf("\nLength of %sis = %d",a,strlen(a)); else
printf("\nLength of %s is= %d",b,strlen(b)); printf("\nStrings are not
printf("\nCopied string = %s",strcpy(c,b)); identical");
printf("\nReversed string = %s",strrev(c)); }
printf("\nUpper case = %s",strupr(c));
convert lowercase characters to uppercase

#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);
}
}

You might also like