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

8_strings

The document provides a comprehensive overview of strings in C programming, detailing their definition, types (constant and modifiable), initialization, memory allocation, size, manipulation, and sharing. It includes examples of string operations, recommended practices for reading strings, and common library functions for string handling. Additionally, it presents DIY exercises and quizzes to reinforce understanding of string concepts.

Uploaded by

Narendra Sv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

8_strings

The document provides a comprehensive overview of strings in C programming, detailing their definition, types (constant and modifiable), initialization, memory allocation, size, manipulation, and sharing. It includes examples of string operations, recommended practices for reading strings, and common library functions for string handling. Additionally, it presents DIY exercises and quizzes to reinforce understanding of string concepts.

Uploaded by

Narendra Sv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Strings

Advanced C
S_____s – Fill in the blanks please ;)

`
Advanced C
Strings

A set of things tied or threaded together on a thin cord


Source: Google

String of Beads

String
Start of String
Bead
Contains n numbers of Beads String ends here
Advanced C
Strings


Contiguous sequence of characters

Stores printable ASCII characters and its extensions

End of the string is marked with a special character, the
null character '\0'

'\0' is implicit in strings enclosed with “”

Example
“You know, now this is what a string is!”
Advanced C
Strings

Constant string
– Also known as string literal
– Such strings are read only
– Usually, stored in read only (code or text segment) area
– String literals are shared

Modifiable String
– Strings that can be modified at run time
– Usually, such strings are stored in modifiable memory
area (data segment, stack or heap)
– Such strings are not shared
Advanced C
Strings - Initialization
001_example.c
char char_array[5] = {'H', 'E', 'L', 'L', 'O'}; Character Array
char str1[6] = {'H', 'E', 'L', 'L', 'O', '\0'}; String
char str2[] = {'H', 'E', 'L', 'L', 'O', '\0'}; Valid
char str3[6] = {“H”, “E”, “L”, “L”, “O”}; Invalid
char str4[6] = {“H” “E” “L” “L” “O”}; Valid
char str5[6] = {“HELLO”}; Valid
char str6[6] = “HELLO”; Valid
char str7[] = “HELLO”; Valid
char *str8 = “HELLO”; Valid
Advanced C
Strings – Memory Allocation
Example
char str1[] = {'H', 'E', 'L', 'L', 'O', '\0'};
char *str2 = “Hello”;

str1 str2
‘H’ 1000 1000 996

‘E’ 1001 ? 997

‘L’ 1002 ? 998

‘L’ 1003 ? 999

‘O’ 1004 ‘H’ 1000

‘\0’ 1005 ‘E’ 1001


‘L’ 1002
‘L’ 1003
‘O’ 1004
‘\0’ 1005
Advanced C
Strings - Size
002_example.c
#include <stdio.h>

int main()
{
char char_array_1[5] = {'H', 'E', 'L', 'L', 'O'}; The size of the array
char char_array_2[] = “Hello”; is calculated so,
sizeof(char_array_1);
sizeof(char_array_2); 5, 6

return 0;
}

003_example.c
int main()
{
char *str = “Hello”; The size of pointer
is always constant
sizeof(str); so,
return 0; 4 (32 Bit Sys)
}
Advanced C
Strings - Size
004_example.c
#include <stdio.h>

int main()
{
if (sizeof(“Hello” “World”) == sizeof(“Hello”) + sizeof(“World”))
{
printf(“WoW\n”);
}
else
{
printf(“HuH\n”);
}

return 0;
}
Advanced C
Strings - Manipulations
005_example.c
#include <stdio.h>
Not possible to assign a string to a
int main()
{ array since its a constant pointer
char str1[6] = “Hello”;
char str2[6];

str2 = “World”;

char *str3 = “Hello”;


char *str4; Possible to assign a string to
a pointer since its variable
str4 = “World”;

str1[0] = 'h';
Valid. str1 contains “hello”
str3[0] = 'w';

printf(“%s\n”, srt1);
printf(“%s\n”, srt2); Invalid. str3 might be stored in
read only section.
return 0; Undefined behaviour
}
Advanced C
Strings - Sharing
006_example.c
#include <stdio.h>

int main()
{
char *str1 = “Hello”;
char *str2 = “Hello”;

if (str1 == str2)
{
printf(“Hoo. They share same space\n”);
}
else
{
printf(“No. They are in different space\n”);
}

return 0;
}
Advanced C
Strings - Sharing

‘H’ 100
‘E’ 101
‘L’ 102
‘L’ 103
‘O’ 104
‘\0’ 105

str1
100 800
? 801

str2
100 996
? 997
? 998
? 999
Advanced C
Strings – Empty String
007_example.c
#include <stdio.h>
#include <string.h>

int main()
{
char *str = “”;
int ret;

ret = strlen(str);
printf(“%d\n”, ret);

return 0;
}
Advanced C
Strings – Passing to Function
008_example.c
#include <stdio.h>

void print(const char *str)


{
while (*str)
{
putchar(*str++);
}
}

int main()
{
char *str = “Hello World”;

print(str);

return 0;
}
Advanced C
Strings - Reading
009_example.c
#include <stdio.h>

int main()
{
char str[6];

gets(str);
printf(“The string is: %s\n”, str);

return 0;
}


The above method is not recommend by the gcc. Will
issue warning while compilation

Might lead to stack smashing if the input length is greater
than array size!!
Advanced C
Strings - Reading
010_example.c
#include <stdio.h>

int main()
{
char str[6];

fgets(str, 6, stdin);
printf(“The string is: %s\n”, str);

scanf(“%5[^\n], str);
printf(“The string is: %s\n”, str);

return 0;
}


fgets() function or selective scan with width are
recommended to read string from the user
Advanced C
Strings – DIY


WAP to calculate length of the string

WAP to copy a string

WAP to compare two strings

WAP to compare two strings ignoring case

WAP to check a given string is palindrome or not
Advanced C
Strings – Library Functions

Purpose Prototype Return Values


Length size_t strlen(const char *str) String Length
Compare int strcmp(const char *str1, const char *str2) str1 < str2 → < 0
str1 > str2 → > 0
str1 = str2 → = 0
Copy char *strcpy(char *dest, const char *src) Pointer to dest
Check char *strstr(const char *haystack, const char *needle) Pointer to the
String beginning of
substring
Check char *strchr(const char *s, int c) Pointer to the
Character matched char else
NULL
Merge char *strcat(char *dest, const char *src) Pointer to dest
Advanced C
Strings – Quiz


Can we copy 2 strings like, str1 = str2?

Why don't we pass the size of the string to string
functions?

What will happen if you overwrite the '\0' (null character)
of string? Will you still call it a string?

What is the difference between char *s and char s[]?
Advanced C
Strings – DIY


WAP to reverse a string

WAP to compare string2 with string1 up to n characters

WAP to concatenate two strings
Advanced C
Strings – DIY


Use the standard string functions like
– strlen
– strcpy
– strcmp
– strcat
– strstr
– strtok
Advanced C
Strings – DIY


WAP to print user information –
– Read : Name, Age, ID, Mobile number
– Print the information on monitor
– Print error “Invalid Mobile Number” if length of mobile
number is not 10

WAP to read user name and password and compare with
stored fields. Present a puzzle to fill in the banks

Use strtok to separate words from string
“www.emertxe.com/bangalore”

You might also like