[go: up one dir, main page]

0% found this document useful (0 votes)
28 views40 pages

Strings Chap 8

rotuirhfyiu

Uploaded by

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

Strings Chap 8

rotuirhfyiu

Uploaded by

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

Strings

String Basics – no. of characters


• printf(“Average = %.2f”, avg);

A v e r a g e = % . 2 f

• A string of 14 characters,
String Basics - no. of characters
• printf(“Hello! Good day”);

H e l l o ! G o o d d a y

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

• A string of 15 characters,
String Basics - Declaration
Case 1:
char vari[10];
vari[10] = “Greeting!”;

Case 2:
char vari[10] = “Greeting!”;
String Basics - Declaration
• char string_var[6] = “value”;
v a l u e 5
Refer to charac
address of [0] ters
[0] [1] [2] [3] [4]
to [4]

• Why we declare string_var array as [6]? Why


not [5]?
String Basics - Declaration
• char string_var[6] = “value”;
6 v a l u e 6
charac
ters

Refer to
address of [0] [] [0] [1] [2] [3] [4]
to [4]

Length-controlled
string
String Basics - Declaration
• char vart[10] = “Hello!”;
End of
10 H e l l o ! \0 string

[] [0] [1] [2] [3] [4] [5] [6]


Address
[0] to [6]

• We declared vart for 10 spaces. What


happen to the rest of the unused address?
String Basics - Declaration
• char vart[10] = “Hello!”;
H e l l o ! \0 ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
Adddress
[0] to [9]
String Basics - Declaration
• char str[20] = “Initial value”;

I n i t i a l v a l u e \0 ? ? ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19]
String Basics - %c and %s - printf
• Case 1: use %c
• Use for loop to print out the string.
for(i=0; i<5; i++)
printf(“%c”, str[i]);

• Case 2: use %s
• For loop is not required.
printf(“%s”, str);
String Basics - %c and %s - scanf
• Case 1: use %c
• Use for loop to store the string in an array.
• A specific address “&” is required.
for (int i=0; i<10; i++ )
scanf("%c", &string_val[i]);

• Case 2: use %s
• Both for loop and & are not required.
scanf(“%s”, str);
String Basics – limitation of scanf
String Basics – Solve %c & %s
limitations
• Function gets() and puts();

printf (“Enter string:”);


gets(str);
puts(str);
String Library – strcpy()
char str01[10], str02[15] = "YYYY";
strcpy(str01, str02);
String copy
to
String Library – strncpy()
char str01[16] = “GREETINGS TO ME”;
char str02[20] = “Greetings to you”;
strncpy(str01, str02, 8);
Copy first 8 characters from
str02 to str01

First 8 characters are from


str02
String Library – strncpy()
char str01[16] = “GREETINGS TO ME”;
char str02[20] = “Greetings to you”;
strncpy(str01, &str02[13], 3);
String Library – strcat()
char str01[16] = “GREETINGS”;
char str02[20] = “ to you”;
strcat(str01, str02);
String Library – strncat()
char str01[16] = “GREETINGS”;
char str02[20] = “ to you”;
strncat(str01, str02, 2);
String Library – strncat()
char str01[16] = “GREETINGS”;
char str02[20] = “ to you”;
strncat(str01, &str02[1], 2);
String Library – strlen()
char str01[16] = “GREETINGS”;
char str02[20] = “ to you”;
printf(“No of Char: %d”,strlen(str01));
String Library – strlen()
char str01[16] = “GREETINGS”;
char str02[20] = “ to you”;
printf(“No of Char: %d”,strlen(str02));
String Library – strcmp()
• Return integer.
– str1 is less than str2, return –ve value;
– str1 equals to str2, return 0;
– str1 is larger than str2, return +ve value;
String Library – strcmp()
char str01[16] = “joyful”;
char str02[20] = “joyous”;
printf(“Compare: %d”,strcmp(str01, str02));
String Library – strcmp()
char str01[16] = “joyful”;
char str02[20] = “joyous”;
printf(“Compare: %d”,strcmp(str02, str01));
String Library – strncmp()
char str01[16] = “joyful”;
char str02[20] = “joyous”;
printf(“Compare: %d”,strncmp(str01, str02, 1));
String Library – strncmp()
char str01[16] = “joyful”;
char str02[20] = “joyous”;
printf(“Compare: %d”,strncmp(str02, str01, 4));
String Library Functions
• Required #include<string.h> in the
header section.
• strcpy(), strncpy(),
• strcmp(), strncmp(),
• strcat(), strncat()
• strlen()
Character Library Functions
• Required ctype.h library in header section.
– getchar(),
– isaplha(),
– isdigit(),
– islower(), isupper,
– ispunct(),
– isspace(),
– tolower(),
– toupper(),
ctype.h function – getchar()
printf("Enter a char:");
scanf(“%c”, &str01);
printf(“%c”, str01);

printf("Enter a char:");
str01 = getchar();
printf("%c", str01);
ctype.h function – isalpha()
char str01;
printf("Enter a char:");
str01 = getchar();
if (isalpha(str01))
printf("It is a %c", str01);
else
printf("It is not a character");
ctype.h function – isdigit()
char str01;
printf("Enter a number:");
str01 = getchar();
if (isdigit(str01))
printf("\nIt is a digit");
else
printf("\nIt is not a digit");
ctype.h function – islower()
char str01;
printf("Enter a char:");
str01 = getchar();
if (islower(str01))
printf("It is a lower case character");
else
printf("It is an upper case character");
ctype.h function – isupper()
char str01;
printf("Enter a char:");
str01 = getchar();
if (isupper(str01))
printf("It is an upper case character");
else
printf("It is a lower case character");
ctype.h function – ispunct()
char str01;
printf("Enter a char:");
str01 = getchar();
if (ispunct(str01))
printf("It is a punctuation");
else
printf("It is not a punctuation");
ctype.h function – isspace()
char str01;
printf("Enter a char:");
str01 = getchar();
if (isspace(str01))
printf("It is a space");
else
printf("It is not a space");
ctype.h function – tolower()
char str01;
printf("Enter a char:");
str01 = getchar();
printf("To lower case: %c", tolower(str01));
ctype.h function – toupper()
char str01;
printf("Enter a char:");
str01 = getchar();
printf("To upper case: %c", toupper(str01));
sprintf() function in stdio.h

• Store and convert the values in string for future


used.

n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);


printf ("[%s] is a string %d chars long\n", buffer,n);
sscanf() function in stdio.h
• Convert and store all variable into string.

sscanf(data_link, “%d%d%s”, &n, &m, str);


THE END

You might also like