SlideShare a Scribd company logo
1 of 21
Bangladesh University ofBusiness & Technology
(BUBT)
Rupnagar , Mirpur-2, Dhaka-1216, Bangladesh
Assignment
o Course Title: Structured Programming Language
o Course Code: CSE 111
o Semester: Summer 2016
o Program: CSE
o Intake: 32nd
o Section: 04
Submitted By : Submitted TO:
Arafat Bin Reza Md. Atiqur Rahman
ID:15162103170 Assistant Professor Dept. of CSE
Phone Number: 01763061221
Strings
WHAT IS STRINGS?
Strings are actually one-dimensional array of characters terminated by
a null character '0'. Thus a null-terminated string contains the characters
that comprise the string followed by a null. These are often used to
create meaningful and readable programs.
Declaringand Initializing a string variables:
There are different ways to initialize a character array variable.
char name [13] = “BUBT CSE "; //valid character array initialization
char name [10] = {‘A’, ‘t’, ‘i’, ‘q’, ‘u',‘r','0’ }; //valid initialization
when you initialize a character array by listings all its characters
separately then you must supply the '0' character explicitly.
We can use pointers to a character array to define simple strings.
char * name = "John Smith";
String Input and Output:
Input function scanf () can be used with %s format specifier to read a
string input from the terminal. But there is one problem
with scanf() function, it terminates its input on first white space it
encounters. Therefore, if you try to read an input string "Hello World"
using scanf() function, it will only read Hello and terminate after
encountering white spaces.
However, C supports a format specification known as the edit set
conversion code %[^n] that can be used to read a line containing a
variety of characters, including white spaces.
Another method to read character string with white spaces from terminal
is gets() function.
Example of string with scanf() function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[20];
printf("Enter a string :n");
scanf("%[^n]",&str);
printf("%s",str);
}
Output:
Example of string with gets () function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[20];
printf("Enter a string");
gets(str);
printf("%s",str);
}
Output:
String Handling Functions:
C language supports a large number of string handling functions that can
be used to carry out many of the string manipulations. These functions
are packaged in string.h library. Hence, you must include string.h header
file in your program to use these functions.
The following are the most commonly used string handling functions.
strcmp () and strcmpi () functions are almost same but the difference
between them is strcmp () function is case sensitive and strcmpi ()
function is not case sensitive.
strcat () function:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "BUBT";
char str2[12] = "CSE";
strcat( str1, str2);
printf("strcat( str1, str2): %sn", str1 );
return 0;
}
Output:
Strcpy () Function:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "BUBT";
char str2[12] = "CSE";
char str3[12];
strcpy(str3, str1);
printf("strcpy( str3, str1) : %sn", str3 );
return 0;
}
Output:
strlen () Function:
#include <stdio.h>
#include <string.h>
int main () {
char str1[12] = "Hello";
char str2[12] = "World";
int len ;
len = strlen(str1);
printf("strlen(str1) : %dn", len );
return 0;
}
Output:
strcmp () Function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20]={"BANGLADESH"};
printf("ENTER YOUR COUNTRY NAME : ");
scanf("%[^n]",&str1);
if(strcmp(str1,str2)==0)
printf("Your Answer Is Right");
else
printf("Your Answer Is Wrong");
getch();
}
Output:
strcmpi () Function:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20]={"BANGLADESH"};
printf("ENTER YOUR COUNTRY NAME : ");
scanf("%[^n]",&str1);
if(strcmpi(str1,str2)==0)
printf("Your Answer Is Right");
else
printf("Your Answer Is Wrong");
getch();
}
Output:
Difference between strcmp Function and strcmpiFunction:
Searching with string:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[100],str2[100]={"bangladesh university of business and
technology"},word[50],a,b,x;
printf("ENTER YOUR UNIVERSITY NAME : ");
gets(str1);
gets(word);
if(strcmp(str1,str2)==0)
{
for(a=0;a<strlen(str1);a++)
{
if(word[0]==str1[a])
{
x=1;
for(b=1;b<strlen(word);b++)
{
if(str1[++a]==word[b])
x++;
else
break;
}
}
if(x==strlen(word))
{
printf("The Word Is Found");
break;
}
}
if(x!=strlen(word))
{
printf("The Word Is Not Found");
}
}
else
printf("Give Your University Name Correctly");
getch();
}
Sorting
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char word[100][100],temp[100];
int i,j,k,p;
printf("How many words you would like to give as an input:");
scanf("%d",&p);
for(i=0; i<p; i++)
scanf("%s",word[i]);
printf("nSortingn");
for (i=0; i<p;i++)
for(j=0;j<p-i-1;j++)
if(strcmp(word[j],word[j+1])>0)
{
strcpy(temp,word[j]);
strcpy(word[j],word[j+1]);
strcpy(word[j+1],temp);
}
for(i=0;i<p;i++)
printf("%st",word[i]);
return 0;
}
Output:
Pointer
WHAT IS Pointer?
Pointers are variables that hold address of another variable of same data
type.
Benefit of using pointers:
 Pointers are more efficient in handling Array and Structure.
 Pointer allows references to function and thereby helps in passing of
function as arguments to other function.
 It reduces length and the program execution time.
 It allows C to support dynamic memory management.

Declaring a pointer variable:
General syntax of pointer declaration is,
data-type *pointer_name;
Data type of pointer must be same as the variable, which the pointer is
pointing. void type pointer works with all data types, but isn't used
oftenly.
Initialization of Pointer variable:
Pointer Initialization is the process of assigning address of a variable
to pointer variable. Pointer variable contains address of variable of same
data type. In C language address operator & is used to determine the
address of a variable. The & (immediately preceding a variable name)
returns the address of the variable associated with it.
int a = 10 ;
int *ptr ; //pointer declaration
ptr = &a ; //pointer initialization
or,
int *ptr = &a ; //initialization and declaration together
Pointer variable always points to same type of data.
float a;
int *ptr;
ptr = &a; //ERROR, type mismatch
Dereferencing of Pointer:
int a,*p;
a = 10;
p = &a;
printf("%d",*p); //this will print the value of a.
printf("%d",*&a); //this will also print the value of a.
printf("%u",&a); //this will print the address of a.
printf("%u",p); //this will also print the address of a.
printf("%u",&p); //this will also print the address of p.
prime number with pointer:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n,c,i;
scanf("%d",&n);
for(i=2;i<=n;i++)
{
if(c=n%i)
c++;
if(c==0)
printf("prime : ");
else
printf("not prime");
}
return 0;
}
Accessing Structure Members with Pointer:
To access members of structure with structure variable, we used the
dot . operator. But when we have a pointer of structure type, we use
arrow -> to access structure members.
struct Book
{
char name[10];
int price;
}
int main()
{
struct Book b;
struct Book* ptr = &b;
ptr->name = "Dan Brown"; //Accessing Structure Members
ptr->price = 500;
}

More Related Content

What's hot

C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branchingSaranya saran
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Hazwan Arif
 
C strings
C stringsC strings
C stringsDucat
 
Data Input and Output
Data Input and OutputData Input and Output
Data Input and OutputSabik T S
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingSabik T S
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshopneosphere
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in CColin
 

What's hot (20)

C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
14 strings
14 strings14 strings
14 strings
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
What is c
What is cWhat is c
What is c
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
C strings
C stringsC strings
C strings
 
Data Input and Output
Data Input and OutputData Input and Output
Data Input and Output
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
C++ string
C++ stringC++ string
C++ string
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
C language basics
C language basicsC language basics
C language basics
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in C
 

Viewers also liked

Viewers also liked (20)

درباره ی بلوبری
درباره ی بلوبریدرباره ی بلوبری
درباره ی بلوبری
 
2 bsci codeofconduct_english_pdf
2 bsci codeofconduct_english_pdf2 bsci codeofconduct_english_pdf
2 bsci codeofconduct_english_pdf
 
Diseño de tablas
Diseño de tablasDiseño de tablas
Diseño de tablas
 
研究生のためのC++ no.4
研究生のためのC++ no.4研究生のためのC++ no.4
研究生のためのC++ no.4
 
..Festival Der Zeppeline
..Festival Der Zeppeline..Festival Der Zeppeline
..Festival Der Zeppeline
 
Music Distribution Presentation
Music Distribution PresentationMusic Distribution Presentation
Music Distribution Presentation
 
E tefl
E teflE tefl
E tefl
 
Reference Pete
Reference PeteReference Pete
Reference Pete
 
研究生のためのC++ no.7
研究生のためのC++ no.7研究生のためのC++ no.7
研究生のためのC++ no.7
 
Rango celdas autorellenar
Rango celdas autorellenarRango celdas autorellenar
Rango celdas autorellenar
 
研究生のためのC++ no.2
研究生のためのC++ no.2研究生のためのC++ no.2
研究生のためのC++ no.2
 
Music Distribution_MVT-SUGO
Music Distribution_MVT-SUGOMusic Distribution_MVT-SUGO
Music Distribution_MVT-SUGO
 
La celebración pedagógica como eje
La celebración pedagógica como ejeLa celebración pedagógica como eje
La celebración pedagógica como eje
 
Taller NTIC
Taller NTICTaller NTIC
Taller NTIC
 
La historia interminable
La historia interminableLa historia interminable
La historia interminable
 
Great ideas in music distribution
Great ideas in music distributionGreat ideas in music distribution
Great ideas in music distribution
 
BSCI (Business Social Compliance Initiative) Code of Conduct & it’s practical...
BSCI (Business Social Compliance Initiative) Code of Conduct & it’s practical...BSCI (Business Social Compliance Initiative) Code of Conduct & it’s practical...
BSCI (Business Social Compliance Initiative) Code of Conduct & it’s practical...
 
Yeny andrea Contreras
Yeny andrea ContrerasYeny andrea Contreras
Yeny andrea Contreras
 
Principles of BSCI
Principles of BSCIPrinciples of BSCI
Principles of BSCI
 
Presentation1 incoterms 2010
Presentation1 incoterms 2010Presentation1 incoterms 2010
Presentation1 incoterms 2010
 

Similar to BUBT Assignment on Strings, Pointers and Structures

Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programmingIcaii Infotech
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and stringsRai University
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Functionimtiazalijoono
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and stringsRai University
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsRai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-aneebkmct
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsRai University
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsRai University
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 

Similar to BUBT Assignment on Strings, Pointers and Structures (20)

Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
C programming
C programmingC programming
C programming
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
String_C.pptx
String_C.pptxString_C.pptx
String_C.pptx
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
String notes
String notesString notes
String notes
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
structure,pointerandstring
structure,pointerandstringstructure,pointerandstring
structure,pointerandstring
 
input
inputinput
input
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
week-6x
week-6xweek-6x
week-6x
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 

More from Arafat Bin Reza

More from Arafat Bin Reza (9)

C# Class Introduction.pptx
C# Class Introduction.pptxC# Class Introduction.pptx
C# Class Introduction.pptx
 
C# Class Introduction
C# Class IntroductionC# Class Introduction
C# Class Introduction
 
Inventory music shop management
Inventory music shop managementInventory music shop management
Inventory music shop management
 
C language 3
C language 3C language 3
C language 3
 
C language 2
C language 2C language 2
C language 2
 
C language updated
C language updatedC language updated
C language updated
 
C language
C languageC language
C language
 
Sudoku solve rmain
Sudoku solve rmainSudoku solve rmain
Sudoku solve rmain
 
final presentation of sudoku solver project
final presentation of sudoku solver projectfinal presentation of sudoku solver project
final presentation of sudoku solver project
 

Recently uploaded

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
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
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
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
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
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

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, ...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
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
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
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
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

BUBT Assignment on Strings, Pointers and Structures

  • 1. Bangladesh University ofBusiness & Technology (BUBT) Rupnagar , Mirpur-2, Dhaka-1216, Bangladesh Assignment o Course Title: Structured Programming Language o Course Code: CSE 111 o Semester: Summer 2016 o Program: CSE o Intake: 32nd o Section: 04 Submitted By : Submitted TO: Arafat Bin Reza Md. Atiqur Rahman ID:15162103170 Assistant Professor Dept. of CSE Phone Number: 01763061221
  • 2. Strings WHAT IS STRINGS? Strings are actually one-dimensional array of characters terminated by a null character '0'. Thus a null-terminated string contains the characters that comprise the string followed by a null. These are often used to create meaningful and readable programs. Declaringand Initializing a string variables: There are different ways to initialize a character array variable. char name [13] = “BUBT CSE "; //valid character array initialization char name [10] = {‘A’, ‘t’, ‘i’, ‘q’, ‘u',‘r','0’ }; //valid initialization when you initialize a character array by listings all its characters separately then you must supply the '0' character explicitly. We can use pointers to a character array to define simple strings. char * name = "John Smith"; String Input and Output: Input function scanf () can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on first white space it encounters. Therefore, if you try to read an input string "Hello World" using scanf() function, it will only read Hello and terminate after encountering white spaces.
  • 3. However, C supports a format specification known as the edit set conversion code %[^n] that can be used to read a line containing a variety of characters, including white spaces. Another method to read character string with white spaces from terminal is gets() function. Example of string with scanf() function: #include<stdio.h> #include<conio.h> #include<string.h> int main() { char str[20]; printf("Enter a string :n"); scanf("%[^n]",&str); printf("%s",str); } Output:
  • 4. Example of string with gets () function: #include<stdio.h> #include<conio.h> #include<string.h> int main() { char str[20]; printf("Enter a string"); gets(str); printf("%s",str); } Output:
  • 5. String Handling Functions: C language supports a large number of string handling functions that can be used to carry out many of the string manipulations. These functions are packaged in string.h library. Hence, you must include string.h header file in your program to use these functions. The following are the most commonly used string handling functions. strcmp () and strcmpi () functions are almost same but the difference between them is strcmp () function is case sensitive and strcmpi () function is not case sensitive.
  • 6. strcat () function: #include <stdio.h> #include <string.h> int main () { char str1[12] = "BUBT"; char str2[12] = "CSE"; strcat( str1, str2); printf("strcat( str1, str2): %sn", str1 ); return 0; } Output:
  • 7. Strcpy () Function: #include <stdio.h> #include <string.h> int main () { char str1[12] = "BUBT"; char str2[12] = "CSE"; char str3[12]; strcpy(str3, str1); printf("strcpy( str3, str1) : %sn", str3 ); return 0; } Output:
  • 8. strlen () Function: #include <stdio.h> #include <string.h> int main () { char str1[12] = "Hello"; char str2[12] = "World"; int len ; len = strlen(str1); printf("strlen(str1) : %dn", len ); return 0; } Output:
  • 9. strcmp () Function: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[20],str2[20]={"BANGLADESH"}; printf("ENTER YOUR COUNTRY NAME : "); scanf("%[^n]",&str1); if(strcmp(str1,str2)==0) printf("Your Answer Is Right"); else printf("Your Answer Is Wrong"); getch(); } Output:
  • 10. strcmpi () Function: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[20],str2[20]={"BANGLADESH"}; printf("ENTER YOUR COUNTRY NAME : "); scanf("%[^n]",&str1); if(strcmpi(str1,str2)==0) printf("Your Answer Is Right"); else printf("Your Answer Is Wrong"); getch(); } Output:
  • 11. Difference between strcmp Function and strcmpiFunction:
  • 12. Searching with string: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[100],str2[100]={"bangladesh university of business and technology"},word[50],a,b,x; printf("ENTER YOUR UNIVERSITY NAME : "); gets(str1); gets(word); if(strcmp(str1,str2)==0) { for(a=0;a<strlen(str1);a++) { if(word[0]==str1[a]) { x=1; for(b=1;b<strlen(word);b++) { if(str1[++a]==word[b])
  • 13. x++; else break; } } if(x==strlen(word)) { printf("The Word Is Found"); break; } } if(x!=strlen(word)) { printf("The Word Is Not Found"); } } else printf("Give Your University Name Correctly"); getch(); }
  • 14. Sorting #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { char word[100][100],temp[100]; int i,j,k,p; printf("How many words you would like to give as an input:"); scanf("%d",&p); for(i=0; i<p; i++) scanf("%s",word[i]); printf("nSortingn"); for (i=0; i<p;i++) for(j=0;j<p-i-1;j++) if(strcmp(word[j],word[j+1])>0) { strcpy(temp,word[j]); strcpy(word[j],word[j+1]); strcpy(word[j+1],temp);
  • 16. Pointer WHAT IS Pointer? Pointers are variables that hold address of another variable of same data type. Benefit of using pointers:  Pointers are more efficient in handling Array and Structure.  Pointer allows references to function and thereby helps in passing of function as arguments to other function.  It reduces length and the program execution time.  It allows C to support dynamic memory management.  Declaring a pointer variable: General syntax of pointer declaration is, data-type *pointer_name; Data type of pointer must be same as the variable, which the pointer is pointing. void type pointer works with all data types, but isn't used oftenly.
  • 17. Initialization of Pointer variable: Pointer Initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type. In C language address operator & is used to determine the address of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with it. int a = 10 ; int *ptr ; //pointer declaration ptr = &a ; //pointer initialization or, int *ptr = &a ; //initialization and declaration together Pointer variable always points to same type of data. float a; int *ptr; ptr = &a; //ERROR, type mismatch
  • 18. Dereferencing of Pointer: int a,*p; a = 10; p = &a; printf("%d",*p); //this will print the value of a. printf("%d",*&a); //this will also print the value of a. printf("%u",&a); //this will print the address of a. printf("%u",p); //this will also print the address of a. printf("%u",&p); //this will also print the address of p.
  • 19. prime number with pointer: #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int n,c,i; scanf("%d",&n); for(i=2;i<=n;i++) { if(c=n%i) c++; if(c==0) printf("prime : "); else printf("not prime"); } return 0; }
  • 20. Accessing Structure Members with Pointer: To access members of structure with structure variable, we used the dot . operator. But when we have a pointer of structure type, we use arrow -> to access structure members. struct Book { char name[10]; int price; } int main() { struct Book b; struct Book* ptr = &b;
  • 21. ptr->name = "Dan Brown"; //Accessing Structure Members ptr->price = 500; }