Computer Programming (CS F111) Strings and Dynamic Memory: BITS Pilani
Computer Programming (CS F111) Strings and Dynamic Memory: BITS Pilani
(CS F111)
Strings and Dynamic memory
BITS Pilani Dr. Lov Kumar, Dr. Sudeepta Mishra, Mr. Abhishek Thakur
Hyderabad Campus
Department of CSIS
Note:
1. Some parts are important from placements perspective as
multiple questions are asked on Strings.
2. Dynamic memory is more from the perspective of handling
structures and linked list – at exposure level only.
https://www.polleverywhere.com/clickable_images/vyAuUprs0F9z2Vj
3 17/Nov/2018 CS F111 BITS Pilani, Hyderabad Campus
CS F111 Second Semester 2011-12
Strings
char str1[17] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r' , ' ' , 'P', 'r', 'o', 'g', 'r', 'a', 'm' , '\0'};
B I T S P I L A N I \0 \0 \0 \0
char text[30];
printf(“Enter a string: ”);
scanf(“%s”,text);
printf(“The string is : %s”,text);
Sample output:
Enter a string: hello
The string is: hello
Enter a string: hello how are you
The string is: hello
Note: scanf() takes string without blank space by default
10 17/Nov/2018 CS F111 BITS Pilani, Hyderabad Campus
Reading a String of specific
kind
char text[30];
printf(“Enter a string: ”);
scanf(“%[a-z]s”,text);
printf(“The string is : %s”,text);
Sample output:
Enter a string: hello
The string is: hello
Enter a string: hello123pla
The string is: hello
12 17/Nov/2018 CS F111 BITS Pilani, Hyderabad Campus
Reading a String : containing only a, b,
c, d
char text[30];
printf(“Enter a string: ”);
scanf(“%[abcd]s”,text);
printf(“The string is : %s”,text);
Sample output:
Enter a string: abbcddsence
The string is: abbcdd
char name[20];
printf(“Enter your name: ”);
scanf(“%[^\n]s”,name);
printf(“The string is : %s\n”,name);
Sample output:
Enter a string: hello
The string is: hello
Enter a string: BITS PILANI
The string is: BITS PILANI
14 17/Nov/2018 CS F111 BITS Pilani, Hyderabad Campus
Reading a String : till newline
char str[100];
fgets(str, 100, stdin);
fputs(str, stdout);
• gets()
• puts()
• unsafe library functions – specially gets – use fgets instead
• can take more no. of characters as input than the
size of the character array
• results in abnormal program behavior/termination
char str[30]={'a','b','\0','c','d'};
printf("\n%s",str); prints only ab
• char
name [12];
name = “BITS PILANI”; Incompatible types in
assignment
• charstr1[6] = “Hello”;
char str2[6];
str2 = str1; Name of string is a pointer
const that cannot be used
as a lvalue
• strlen(str)
• strcat(str1, str2) safe option strncat()
char *strncat(char *dest, const char *src, size_t n);
int main()
{
char name1[15] = "ooooooooo";
char name2[8] = "pppppp";
strcpy(name1, name2);
printf("%s\n",name1);
OUTPUT:
pppppp
#include<stdio.h>
#include<string.h>
int main()
{
char name1[15] = "abcd";
char name2[8] = "xyzw";
strcat(name1,strcpy(name1, name2));
printf("%s\n",name1);
OUTPUT:
xyzwxyzw
int main()
{
char name1[15]="abcefgh";
char name2[]="xyz";
char name3[]="efg";
strcpy(name1,name2);
printf("\n%s\n%c",name1,name1[5]);
}
Output---
xyz
g
#include<stdio.h>
#include<string.h>
int main()
{
char name1[15]="abcefgh";
char name2[]="xyz";
printf(“\n %d”,strcmp(name1,name2));
}
Output ---
-23 //(name1 - name2)
• strncmp(str1, str2, n)
• compares at most the first n characters of str1 and str2
• strcmpi(str1, str2)
• compares str1 and str2 by ignoring case
• strncmpi(str1, str2, n)
• compares at most the first n characters of str1 and str2 by
ignoring case
• How to declare it :
data-type *arrayName[SIZE];
int main()
{
char *month[] = {"January", "February", "March", "April",
"May", "June", "July", "August" };
int i, j;
for(i = 0; i < 5; i++)
printf("\n%s",month[i]);
for(i = 0; i < 5; i++)
for(j = 0; month[i][j] != '\0'; j++)
printf("\n%c", month[i][j]);
return 0;
}
For example
String 1 : “123454321”
String 2 : “128454351”
For example :
s1 : “australia”
s2 : “utsralaai”
• Declaration :
char words[50][30];
; // declaration
• returns the base address
• memory initialized to 0
; // function declaration
; // declaration
• Changes the size
• If new size > old size then new space is allocated
• previous contents are copied
• remaining are uninitialized (may be 0 or garbage
value)
• If request cannot be satisfied then p does not change
• If new size < old size, same space is reallocated
• returns pointer to the new space
If you write
int * a = (int *)malloc(n * sizeof(int));
a = realloc(a, 0);
it is as if you are freeing memory
values present in old block will become uninitialized (0 or
garbage values)
After statement a = realloc(a, 0);
if you try to access the elements as pointed to by a, you may
get a segmentation fault.
int main()
{
int **a, i;
a = (int **)malloc(4*sizeof(int));
for(i=0;i<4;i++)
a[i] = (int *)malloc(5*sizeof(int));
for(i=0;i<4;i++)
{
free(a[i]);
}
free(a);
return 0;
}
But if you try to print b[i] inside the loop, you will get
segmentation fault