SlideShare a Scribd company logo
1 of 21
KEYPOINTS
STRING HANDLING & DYNAMIC
MEMORY ALLOCATION
PREPARED BY
AKILA K
ASSISTANT PROFESSOR
DEPT-CSE
RMKCET
STRING DECLARATIONS
char *p = "String";
char p2[] = "String";
char p3[5] = "String";
char arr[7]={‘s',‘t',‘r',‘i',‘n‘,‘g'};
scanf("%s",str); // to read the string
Printf(“%s”,str); // to print the string-
String Declaration
What’s the difference between:
char amessage[ ] = "message"
char *pmessage = "message"
Answer:
char amessage[ ] = "message" // single array
Cannot be reassigned to point to another string „
• The array of characters are located on the stack
• can be mutated i.e. you can change the string contents
char *pmessage = "message" // pointer and array
It is pointer that points to first location of the string
„Pointer can be reassigned to point to another string „
Cannot change the string contents (immutable) „The string is placed in a “read only” section
Strlib.h
Example
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
strcpy(str3, str1);
printf("strcpy( str3, str1) : %sn", str3 );
strcat( str1, str2);
printf("strcat( str1, str2): %sn", str1 );
len = strlen(str1);
printf("strlen(str1) : %dn", len );
char *a=strchr(str1, 'W');
printf("strchar: %s",a);
char *ab=strstr(str1, "lo");
printf("strchar: %s",ab);
Example
int strlen(char str[])
{
int i = 0;
while (str[i] != ‘0’)
{
i++;
}
return i;
}
int strlen(const char* str)
{
int i = 0;
while (*str != ‘0’)
{
i++;
str++;
}
return i;
}
String separation
#include <string.h>
#include <stdio.h>
int main ()
{ char str[80] = "This is - www.tutorialspoint.com - website";
const char s[2] = "-";
char *token;
token = strtok(str, s);
while( token != NULL )
{
printf( " %sn", token );
token = strtok(NULL, s);
}
return(0);
}
const keyword
const int x; // constant int
x = 2; // illegal - can't modify x
const int* pX; // changeable pointer to constant int
*pX = 3; // illegal - can't use pX to modify an int
pX = &someOtherIntVar; // legal - pX can point somewhere else
int* const pY; // constant pointer to changeable int
*pY = 4; // legal - can use pY to modify an int
pY = &someOtherIntVar; // illegal - can't make pY point anywhere else
const int* const pZ; // const pointer to const int
*pZ = 5; // illegal - can't use pZ to modify an int
pZ = &someOtherIntVar; // illegal - can't make pZ point anywhere else
Constant pointer - a constant pointer already pointing to an address, cannot
point to a new address.
Pointer to Constant -type of pointer cannot change the value at the address
pointed by it.
char ch, c;
char *ptr = &ch
ptr = &c
char ch = 'c';
char c = 'a';
char *const ptr = &ch; // A constant pointer
ptr = &c; // wrong
char ch = 'c';
char *ptr = &ch
*ptr = 'a';
char ch = 'c';
const char *ptr = &ch; // A constant pointer
*ptr = 'a';// WRONG!!!
Pointer to Pointer
char **p1 = NULL;
char *p2 = NULL;
char value = 'c';
p2 = &value;
p1 = &p2;
printf("n value = [%c]n",value);
printf("n *p2 = [%c]n",*p2);
printf("n p2 = [%lu]n",p2);
printf("n **p1 = [%c]n",**p1);
printf("n p1 = [%lu]n",p1);
Output:
value = [c]
*p2 = [c]
p2 = [8000]
**p1 = [c]
p1 = [5000]
Array of Pointers
char *p1 = "Himanshu";
char *p2 = "Arora";
char *p3 = "India";
char *arr[3];
arr[0] = p1;
arr[1] = p2;
arr[2] = p3;
printf("n p1 = [%s] n",p1);
printf("n p2 = [%s] n",p2);
printf("n p3 = [%s] n",p3);
printf("n arr[0] = [%s] n",arr[0]);
printf("n arr[1] = [%s] n",arr[1]);
printf("n arr[2] = [%s] n",arr[2]);
Function Pointers
int func (int a, int b)
{
printf("n a = %dn“,a);
printf("n b = %dn“,b);
return 0;
}
int main(void)
{
int(*fptr)(int,int); // Function pointer
fptr = func; // Assign address to function pointer func(2,3);
fptr(2,3);
return 0;
}
ASCII VALUES
v
VOWELS & CONSONANTS
scanf("%s",str);
while(str[i]!='0')
{
if (str[i]=='a' || str[i]=='e' || str[i]=='i'
|| str[i]=='o' || str[i]=='u' )
{
c=str[i];
c++;
s[i]=(char)c;
}
else if(str[i]>=98 && str[i]<=100)
{
s[i]='e';
}
else if(str[i]>=102 && str[i]<=104)
{
s[i]='i';
}
else if(str[i]>=105 && str[i]<=109)
{
s[i]='o';
}
else if(str[i]>=111 &&
str[i]<=115)
{
s[i]='u';
}
else
{
s[i]='a';
}
i++;
}
printf("%s",s);
return 0;
}
Encryption
char st[100];
int x,y,i=0;
fgets(st,100,stdin);
scanf("%d %d",&x,&y);
for(i=0;st[i]!='0';i++)
{
if (st[i]>=97 && st[i]<=122)
printf("%c",st[i]+x);
else if (st[i]>=48 && st[i]<=57)
printf("%c",st[i]+y);
else
printf("%c",st[i]);
}
Input:
hello 101fm
1
1
Output:
ifmmp 212gn
lexicographical order
char str[10][50], temp[50];
printf("Enter 10 words:n");
for(i=0; i<10; ++i)
scanf("%s[^n]",str[i])
for(i=0; i<9; i++)
for(j=i+1; j<9 ; j++)
{
if(strcmp(str[i], str[j])>0)
{
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}}
printf("nIn lexicographical order: n");
for(i=0; i<10; ++i) { puts(str[i]); }
DYNAMIC MEMORY ALLOCATION
malloc() allocates single block of requested memory.
Initializes the allocated memory with garbage values.
(cast_type *)malloc(Size_in_bytes); // 1 argument
calloc() allocates multiple block of requested memory.
initializes the allocated memory with 0 value.
(cast_type *)calloc(blocks , size_of_block); // 2 arguments
realloc() reallocates the memory occupied by malloc() or calloc()
functions.
free() frees the dynamically allocated memory
4 functions of stdlib.h
int *x;
x = (int*)malloc(50 * sizeof(int));
free(x)
struct employee {
char *name;
int salary; };
typedef struct employee emp;
emp *e1;
e1 = (emp*)calloc(30,sizeof(emp));
int *x;
x = (int*)malloc(50 * sizeof(int));
x = (int*)realloc(x,100);
STRUCTURE AND UNION
• `
#include <stdio.h>
union unionJob
{
char name[32]; float salary; int workerNo;
} uJob;
struct structJob
{ char name[32]; float salary;
int workerNo;
} sJob;
int main() {
printf("size of union = %d", sizeof(uJob));
printf("nsize of structure = %d", sizeof(sJob));
return 0; }
#include <stdio.h>
union job {
char name[32];
float salary;
int workerNo; } job1;
int main() {
printf("Enter name:n");
scanf("%s", &job1.name);
printf("Enter salary: n");
scanf("%f", &job1.salary);
printf("DisplayingnName :%sn", job1.name);
printf("Salary: %.1f", job1.salary); return 0; }
Enter name Hillary
Enter salary 1234.23
Displaying
Name: f%Bary
Salary: 1234.2
You may get different garbage value or empty string for the name.

More Related Content

What's hot (20)

Pointer in C
Pointer in CPointer in C
Pointer in C
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Ponters
PontersPonters
Ponters
 
Lyntale: MS Code Contracts
Lyntale: MS Code ContractsLyntale: MS Code Contracts
Lyntale: MS Code Contracts
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Data structure week 2
Data structure week 2Data structure week 2
Data structure week 2
 
Safe int
Safe intSafe int
Safe int
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointer
PointerPointer
Pointer
 
Unitii string
Unitii stringUnitii string
Unitii string
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Buffer OverFlow
Buffer OverFlowBuffer OverFlow
Buffer OverFlow
 
Unit 8. Pointers
Unit 8. PointersUnit 8. Pointers
Unit 8. Pointers
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 

Similar to Keypoints c strings

Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxajajkhan16
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfsudhakargeruganti
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptgeorgejustymirobi1
 
NRG 106_Session 6_String&Pointer.pptx
NRG 106_Session 6_String&Pointer.pptxNRG 106_Session 6_String&Pointer.pptx
NRG 106_Session 6_String&Pointer.pptxKRIPABHARDWAJ1
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointerNishant Munjal
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
 
Here is the code in text format stringhelp-h #pragma once #ifndef STRI.docx
Here is the code in text format stringhelp-h #pragma once #ifndef STRI.docxHere is the code in text format stringhelp-h #pragma once #ifndef STRI.docx
Here is the code in text format stringhelp-h #pragma once #ifndef STRI.docxJuliang56Parsonso
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxRamakrishna Reddy Bijjam
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptxcricketreview
 

Similar to Keypoints c strings (20)

Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
iit c prog.ppt
iit c prog.pptiit c prog.ppt
iit c prog.ppt
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
c programming
c programmingc programming
c programming
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
NRG 106_Session 6_String&Pointer.pptx
NRG 106_Session 6_String&Pointer.pptxNRG 106_Session 6_String&Pointer.pptx
NRG 106_Session 6_String&Pointer.pptx
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
C programming
C programmingC programming
C programming
 
Here is the code in text format stringhelp-h #pragma once #ifndef STRI.docx
Here is the code in text format stringhelp-h #pragma once #ifndef STRI.docxHere is the code in text format stringhelp-h #pragma once #ifndef STRI.docx
Here is the code in text format stringhelp-h #pragma once #ifndef STRI.docx
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
 
C programm.pptx
C programm.pptxC programm.pptx
C programm.pptx
 

More from Akila Krishnamoorthy

More from Akila Krishnamoorthy (13)

Automata Theory - Turing machine
Automata Theory - Turing machineAutomata Theory - Turing machine
Automata Theory - Turing machine
 
Automata theory - RE to DFA Conversion
Automata theory - RE to DFA ConversionAutomata theory - RE to DFA Conversion
Automata theory - RE to DFA Conversion
 
Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)
 
Automata theory -RE to NFA-ε
Automata theory -RE to  NFA-εAutomata theory -RE to  NFA-ε
Automata theory -RE to NFA-ε
 
Automata theory - NFA ε to DFA Conversion
Automata theory - NFA ε to DFA ConversionAutomata theory - NFA ε to DFA Conversion
Automata theory - NFA ε to DFA Conversion
 
Automata theory - NFA to DFA Conversion
Automata theory - NFA to DFA ConversionAutomata theory - NFA to DFA Conversion
Automata theory - NFA to DFA Conversion
 
Automata theory -- NFA and DFA construction
Automata theory -- NFA and DFA  constructionAutomata theory -- NFA and DFA  construction
Automata theory -- NFA and DFA construction
 
Intro to automata theory
Intro to automata theoryIntro to automata theory
Intro to automata theory
 
Automata theory -Conversion of ε nfa to nfa
Automata theory -Conversion of ε nfa to nfaAutomata theory -Conversion of ε nfa to nfa
Automata theory -Conversion of ε nfa to nfa
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Slr parser
Slr parserSlr parser
Slr parser
 
CLR AND LALR PARSER
CLR AND LALR PARSERCLR AND LALR PARSER
CLR AND LALR PARSER
 
Linear data structure concepts
Linear data structure conceptsLinear data structure concepts
Linear data structure concepts
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

Keypoints c strings

  • 1. KEYPOINTS STRING HANDLING & DYNAMIC MEMORY ALLOCATION PREPARED BY AKILA K ASSISTANT PROFESSOR DEPT-CSE RMKCET
  • 2. STRING DECLARATIONS char *p = "String"; char p2[] = "String"; char p3[5] = "String"; char arr[7]={‘s',‘t',‘r',‘i',‘n‘,‘g'}; scanf("%s",str); // to read the string Printf(“%s”,str); // to print the string-
  • 3. String Declaration What’s the difference between: char amessage[ ] = "message" char *pmessage = "message" Answer: char amessage[ ] = "message" // single array Cannot be reassigned to point to another string „ • The array of characters are located on the stack • can be mutated i.e. you can change the string contents char *pmessage = "message" // pointer and array It is pointer that points to first location of the string „Pointer can be reassigned to point to another string „ Cannot change the string contents (immutable) „The string is placed in a “read only” section
  • 5. Example char str1[12] = "Hello"; char str2[12] = "World"; char str3[12]; int len ; strcpy(str3, str1); printf("strcpy( str3, str1) : %sn", str3 ); strcat( str1, str2); printf("strcat( str1, str2): %sn", str1 ); len = strlen(str1); printf("strlen(str1) : %dn", len ); char *a=strchr(str1, 'W'); printf("strchar: %s",a); char *ab=strstr(str1, "lo"); printf("strchar: %s",ab);
  • 6. Example int strlen(char str[]) { int i = 0; while (str[i] != ‘0’) { i++; } return i; } int strlen(const char* str) { int i = 0; while (*str != ‘0’) { i++; str++; } return i; }
  • 7. String separation #include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.tutorialspoint.com - website"; const char s[2] = "-"; char *token; token = strtok(str, s); while( token != NULL ) { printf( " %sn", token ); token = strtok(NULL, s); } return(0); }
  • 8. const keyword const int x; // constant int x = 2; // illegal - can't modify x const int* pX; // changeable pointer to constant int *pX = 3; // illegal - can't use pX to modify an int pX = &someOtherIntVar; // legal - pX can point somewhere else int* const pY; // constant pointer to changeable int *pY = 4; // legal - can use pY to modify an int pY = &someOtherIntVar; // illegal - can't make pY point anywhere else const int* const pZ; // const pointer to const int *pZ = 5; // illegal - can't use pZ to modify an int pZ = &someOtherIntVar; // illegal - can't make pZ point anywhere else
  • 9. Constant pointer - a constant pointer already pointing to an address, cannot point to a new address. Pointer to Constant -type of pointer cannot change the value at the address pointed by it. char ch, c; char *ptr = &ch ptr = &c char ch = 'c'; char c = 'a'; char *const ptr = &ch; // A constant pointer ptr = &c; // wrong char ch = 'c'; char *ptr = &ch *ptr = 'a'; char ch = 'c'; const char *ptr = &ch; // A constant pointer *ptr = 'a';// WRONG!!!
  • 10. Pointer to Pointer char **p1 = NULL; char *p2 = NULL; char value = 'c'; p2 = &value; p1 = &p2; printf("n value = [%c]n",value); printf("n *p2 = [%c]n",*p2); printf("n p2 = [%lu]n",p2); printf("n **p1 = [%c]n",**p1); printf("n p1 = [%lu]n",p1); Output: value = [c] *p2 = [c] p2 = [8000] **p1 = [c] p1 = [5000]
  • 11. Array of Pointers char *p1 = "Himanshu"; char *p2 = "Arora"; char *p3 = "India"; char *arr[3]; arr[0] = p1; arr[1] = p2; arr[2] = p3; printf("n p1 = [%s] n",p1); printf("n p2 = [%s] n",p2); printf("n p3 = [%s] n",p3); printf("n arr[0] = [%s] n",arr[0]); printf("n arr[1] = [%s] n",arr[1]); printf("n arr[2] = [%s] n",arr[2]);
  • 12. Function Pointers int func (int a, int b) { printf("n a = %dn“,a); printf("n b = %dn“,b); return 0; } int main(void) { int(*fptr)(int,int); // Function pointer fptr = func; // Assign address to function pointer func(2,3); fptr(2,3); return 0; }
  • 14. VOWELS & CONSONANTS scanf("%s",str); while(str[i]!='0') { if (str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' ) { c=str[i]; c++; s[i]=(char)c; } else if(str[i]>=98 && str[i]<=100) { s[i]='e'; } else if(str[i]>=102 && str[i]<=104) { s[i]='i'; } else if(str[i]>=105 && str[i]<=109) { s[i]='o'; } else if(str[i]>=111 && str[i]<=115) { s[i]='u'; } else { s[i]='a'; } i++; } printf("%s",s); return 0; }
  • 15. Encryption char st[100]; int x,y,i=0; fgets(st,100,stdin); scanf("%d %d",&x,&y); for(i=0;st[i]!='0';i++) { if (st[i]>=97 && st[i]<=122) printf("%c",st[i]+x); else if (st[i]>=48 && st[i]<=57) printf("%c",st[i]+y); else printf("%c",st[i]); } Input: hello 101fm 1 1 Output: ifmmp 212gn
  • 16. lexicographical order char str[10][50], temp[50]; printf("Enter 10 words:n"); for(i=0; i<10; ++i) scanf("%s[^n]",str[i]) for(i=0; i<9; i++) for(j=i+1; j<9 ; j++) { if(strcmp(str[i], str[j])>0) { strcpy(temp, str[i]); strcpy(str[i], str[j]); strcpy(str[j], temp); }} printf("nIn lexicographical order: n"); for(i=0; i<10; ++i) { puts(str[i]); }
  • 17. DYNAMIC MEMORY ALLOCATION malloc() allocates single block of requested memory. Initializes the allocated memory with garbage values. (cast_type *)malloc(Size_in_bytes); // 1 argument calloc() allocates multiple block of requested memory. initializes the allocated memory with 0 value. (cast_type *)calloc(blocks , size_of_block); // 2 arguments realloc() reallocates the memory occupied by malloc() or calloc() functions. free() frees the dynamically allocated memory 4 functions of stdlib.h
  • 18. int *x; x = (int*)malloc(50 * sizeof(int)); free(x) struct employee { char *name; int salary; }; typedef struct employee emp; emp *e1; e1 = (emp*)calloc(30,sizeof(emp)); int *x; x = (int*)malloc(50 * sizeof(int)); x = (int*)realloc(x,100);
  • 20. #include <stdio.h> union unionJob { char name[32]; float salary; int workerNo; } uJob; struct structJob { char name[32]; float salary; int workerNo; } sJob; int main() { printf("size of union = %d", sizeof(uJob)); printf("nsize of structure = %d", sizeof(sJob)); return 0; }
  • 21. #include <stdio.h> union job { char name[32]; float salary; int workerNo; } job1; int main() { printf("Enter name:n"); scanf("%s", &job1.name); printf("Enter salary: n"); scanf("%f", &job1.salary); printf("DisplayingnName :%sn", job1.name); printf("Salary: %.1f", job1.salary); return 0; } Enter name Hillary Enter salary 1234.23 Displaying Name: f%Bary Salary: 1234.2 You may get different garbage value or empty string for the name.