L18 L19 Strings
L18 L19 Strings
STRINGS
Objectives
For example
✓The 5 characters that compose the word "Hello" plus a final null character
('\0') which specifies the end of the sequence and that,
✓In the second case, when using double quotes (") null character ('\0') is
appended automatically.
int main() {
}
5/7/2022 CSE 1051 Department of CSE 10
Example
#include <stdio.h>
int main() {
const int MAX = 80;//max characters in string
char str[MAX]; //string variable str
To read everything that you enter from the keyboard until the ENTER
key is pressed (including space).
Syntax:
gets(stringname) ;
To write/display that you entered.
Syntax:
puts(stringname) ;
5/7/2022 CSE 1051 Department of CSE 12
Example
#include <stdio.h>
int main() {
const int MAX = 80; //max characters in string
puts(sent);
5/7/2022 CSE 1051 Department of CSE 14
Count the number of words in a sentence
#include <stdio.h> while(sent[i]!='\0')
int main() { {
const int MAX = 100; if(sent[i]==' '&& sent[i+1]!=' ')
char sent[MAX];
count++;
int i=0,count=1;
i++;
}
printf("enter sentence \n“);
printf(“No.of words = %d“, count);
gets(sent);
return 0;
printf("\n“);
}
5/7/2022 CSE 1051 Department of CSE 15
Reading multiple lines: Example
#include <stdio.h>
int main() {
const int MAX = 2000; //max characters in string
char str[MAX]; //string variable str
printf("\nEnter a string:\n“);
scanf("%[^#]",str); //read characters to str until a # character is encountered
printf("You entered:\n“);
printf(“%s”,str);
return 0;
} The function will continue to accept characters until termination key (#) is pressed.
5/7/2022 CSE 1051 Department of CSE 16
Library functions: String Handling functions (built-in)
• Used to manipulate a given string.
• These functions are part of string.h header file.
▪strlen ()
✓ gives the length of the string. E.g. strlen(string)
▪strcpy ()
✓ copies one string to other. E.g. strcpy(Dstr1, Sstr2)
▪strcmp ()
✓ compares the two strings. E.g. strcmp(str1, str2)
▪strcat ()
✓Concatinate the two strings. E.g. strcat(str1, str2)
will assign the contents of the string variable city2 to the string variable city1.
The size of the array city1 should be large enough to receive the contents of city2.
5/7/2022 CSE 1051 Department of CSE 20
strcpy(): Example
#include <stdio.h>
int main(){
}
5/7/2022 CSE 1051 Department of CSE 21
Library function: strcmp()
▪ The strcmp function compares two strings identified by the arguments and has a value 0
if they are equal.
strcat(string1, string2);
string1 and string2 are character arrays.
✓ When the function strcat is excuted, string2 is appended to string1.
✓ It does so by removing the null character at the end of string1 and placing string2 from
there.
✓ The string at string2 remains unchanged.
strcat(s1, s2);
printf("\nConcatenated string is“);
printf(“%s”,s1);
return 0; }
5/7/2022 CSE 1051 Department of CSE 24
Strings
Declaration and initialization
char string_name[size];
The size determines the number of characters in the string_name.
For example, consider the following array:
char name [20];
is an array that can store up to 20 elements of type char.
It can be declared, initialized & represented as:
int main() {
char pw[10],un[10], ch; /*print the entered password*/
int i; printf("\nYour password is : ");
printf("Enter User name: "); puts(pw);
gets(un); return 0;
printf("Enter password <6 char>:"); }
for(i=0;i<6;i++) {
pw[i] = getch();
printf("*");
}
5/7/2022 CSE 1051 Department of CSE 29
Print an alphabet in decimal [ASCII] & character form
for(c=65;c<=122;c++) return 0;
}
{
if(c>90 && c<97)
continue;
5/7/2022 CSE 1051 Department of CSE 30
Change all lower case letters into uppercase in a sentence
for(i=0;i<n;i++)
int main()
{
{
char string[30];
if(string[i]>=97 && string[i]<=122)
int main() {
int i=0,j=0,k=0,count=0; Enter main string:- a cat is a cat that is a cat
int l=0,k1=0,cn[10],c=0; Enter sub-string : cat
char a[80],b[80]; Substring is present 3 time(s) at position(s) 3 12 26