SlideShare uma empresa Scribd logo
1 de 4
Baixar para ler offline
Practice codes for CSC- 391
ARRAY< FUNCTION< POINTERS
1. Passing pointer to array
#include <stdio.h>
#include <time.h>
void getSeconds(unsigned long *par);
int main ()
{
unsigned long sec;
getSeconds( &sec );
/* print the actual value */
printf("Number of seconds: %ldn", sec );
return 0;
}
void getSeconds(unsigned long *par)
{
/* get the current number of seconds */
*par = time( NULL );
return;
}
2. passing array to a function
#include <stdio.h>
#include <stdio.h>
double getAverage(int *arr, int size);
int main ()
{
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
/* pass pointer to the array as an argument */
avg = getAverage( balance, 5 ) ;
/* output the returned value */
printf("Average value is: %fn", avg );
return 0;
}
double getAverage(int *arr, int size)
{
int i, sum = 0;
double avg;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
avg = (double)sum / size;
return avg;
}
// double getAverage(int arr[], int size)
{
int i;
double avg;
double sum;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
avg = sum / size;
return avg;
}
Some methods in string.h:
S.N. Function & Purpose
1
strcpy(s1, s2);
Copies string s2 into string s1.
2
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3
strlen(s1);
Returns the length of string s1.
4
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5
strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6
strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
3. Using string methods:
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %sn", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %sn", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %dn", len );
return 0;
}
4. Returning pointer from a function
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* function to generate and retrun random numbers. */
int * getRandom( )
{
static int r[10];
int i;
for ( i = 0; i < 10; ++i)
{
r[i] = rand();
printf("%dn", r[i] );
}
return r;
}
/* main function to call above defined function */
int main ()
{
int *p;
int i;
p = getRandom();
for ( i = 0; i < 10; i++ )
{
printf("*(p + [%d]) : %dn", i, *(p + i) );
}
return 0;
}
Pattern Matching
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char T[100]="Pattern Matching Algorithm";
char P[20]="Pattern";
int r,s,i, k=0, max, index;
s=strlen(T);
r=strlen(P);
max= s-r;
while(k<=max)
{
for(i=0; i<r; i++)
{
if(P[i]!=T[k+i]) break;
}
if(i==R)
{
index=k;
break;
}
else k=k+1;
}
if(k>max) index=-1;
printf("T = %s", T);
printf("nP = %s", P);
if(index!=-1) printf("nn Index of P in T is %d", index);
else printf("n P does not occur in T");
getch();
return 0;
}

Mais conteúdo relacionado

Mais procurados

Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
Moriyoshi Koizumi
 

Mais procurados (20)

C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
week-16x
week-16xweek-16x
week-16x
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
C++ programs
C++ programsC++ programs
C++ programs
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
String searching
String searchingString searching
String searching
 
C++ Programming - 3rd Study
C++ Programming - 3rd StudyC++ Programming - 3rd Study
C++ Programming - 3rd Study
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Function basics
Function basicsFunction basics
Function basics
 
About Go
About GoAbout Go
About Go
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Linear queue
Linear queueLinear queue
Linear queue
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
 
C questions
C questionsC questions
C questions
 

Semelhante a array, function, pointer, pattern matching

booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
GkhanGirgin3
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
adampcarr67227
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
jillisacebi75827
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
arshiartpalace
 

Semelhante a array, function, pointer, pattern matching (20)

Tharun prakash.pptx
Tharun prakash.pptxTharun prakash.pptx
Tharun prakash.pptx
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
manipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptxmanipulation of Strings+PPT.pptx
manipulation of Strings+PPT.pptx
 
Unitii string
Unitii stringUnitii string
Unitii string
 
strings
stringsstrings
strings
 
c programming
c programmingc programming
c programming
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
The best every notes on c language is here check it out
The best every notes on c language is here check it outThe best every notes on c language is here check it out
The best every notes on c language is here check it out
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functions
 
httplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docxhttplinux.die.netman3execfork() creates a new process by.docx
httplinux.die.netman3execfork() creates a new process by.docx
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
C q 3
C q 3C q 3
C q 3
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
Frequency .java Word frequency counter package frequ.pdf
Frequency .java  Word frequency counter  package frequ.pdfFrequency .java  Word frequency counter  package frequ.pdf
Frequency .java Word frequency counter package frequ.pdf
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 

Mais de Shakila Mahjabin

Mais de Shakila Mahjabin (15)

Computer processing
Computer processingComputer processing
Computer processing
 
Arrays in CPP
Arrays in CPPArrays in CPP
Arrays in CPP
 
CSC 433 Sample normalization SQL Question
CSC 433 Sample normalization SQL QuestionCSC 433 Sample normalization SQL Question
CSC 433 Sample normalization SQL Question
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Normalization
NormalizationNormalization
Normalization
 
Solution of Erds
Solution of ErdsSolution of Erds
Solution of Erds
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
 
Ch1- Introduction to dbms
Ch1- Introduction to dbmsCh1- Introduction to dbms
Ch1- Introduction to dbms
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Algo analysis
Algo analysisAlgo analysis
Algo analysis
 
Merge sort and quick sort
Merge sort and quick sortMerge sort and quick sort
Merge sort and quick sort
 
Codes on structures
Codes on structuresCodes on structures
Codes on structures
 
Arrays
ArraysArrays
Arrays
 
String operation
String operationString operation
String operation
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 

Último

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Último (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

array, function, pointer, pattern matching

  • 1. Practice codes for CSC- 391 ARRAY< FUNCTION< POINTERS 1. Passing pointer to array #include <stdio.h> #include <time.h> void getSeconds(unsigned long *par); int main () { unsigned long sec; getSeconds( &sec ); /* print the actual value */ printf("Number of seconds: %ldn", sec ); return 0; } void getSeconds(unsigned long *par) { /* get the current number of seconds */ *par = time( NULL ); return; } 2. passing array to a function #include <stdio.h> #include <stdio.h> double getAverage(int *arr, int size); int main () { int balance[5] = {1000, 2, 3, 17, 50}; double avg; /* pass pointer to the array as an argument */ avg = getAverage( balance, 5 ) ; /* output the returned value */ printf("Average value is: %fn", avg );
  • 2. return 0; } double getAverage(int *arr, int size) { int i, sum = 0; double avg; for (i = 0; i < size; ++i) { sum += arr[i]; } avg = (double)sum / size; return avg; } // double getAverage(int arr[], int size) { int i; double avg; double sum; for (i = 0; i < size; ++i) { sum += arr[i]; } avg = sum / size; return avg; } Some methods in string.h: S.N. Function & Purpose 1 strcpy(s1, s2); Copies string s2 into string s1. 2 strcat(s1, s2); Concatenates string s2 onto the end of string s1. 3 strlen(s1); Returns the length of string s1. 4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. 5 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1. 6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1. 3. Using string methods: #include <stdio.h> #include <string.h> int main () { char str1[12] = "Hello"; char str2[12] = "World";
  • 3. char str3[12]; int len ; /* copy str1 into str3 */ strcpy(str3, str1); printf("strcpy( str3, str1) : %sn", str3 ); /* concatenates str1 and str2 */ strcat( str1, str2); printf("strcat( str1, str2): %sn", str1 ); /* total lenghth of str1 after concatenation */ len = strlen(str1); printf("strlen(str1) : %dn", len ); return 0; } 4. Returning pointer from a function #include <stdio.h> #include <stdlib.h> #include <time.h> /* function to generate and retrun random numbers. */ int * getRandom( ) { static int r[10]; int i; for ( i = 0; i < 10; ++i) { r[i] = rand(); printf("%dn", r[i] ); } return r; } /* main function to call above defined function */ int main () { int *p; int i; p = getRandom(); for ( i = 0; i < 10; i++ ) {
  • 4. printf("*(p + [%d]) : %dn", i, *(p + i) ); } return 0; } Pattern Matching #include<stdio.h> #include<conio.h> #include<string.h> int main() { char T[100]="Pattern Matching Algorithm"; char P[20]="Pattern"; int r,s,i, k=0, max, index; s=strlen(T); r=strlen(P); max= s-r; while(k<=max) { for(i=0; i<r; i++) { if(P[i]!=T[k+i]) break; } if(i==R) { index=k; break; } else k=k+1; } if(k>max) index=-1; printf("T = %s", T); printf("nP = %s", P); if(index!=-1) printf("nn Index of P in T is %d", index); else printf("n P does not occur in T"); getch(); return 0; }