string , pointer

24
Bangladesh University of Business & 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: 32 nd o Section: 04 Submitted By : Submitted TO:

Transcript of string , pointer

Page 1: string , pointer

Bangladesh University of Business & Technology

(BUBT)

Rupnagar , Mirpur-2, Dhaka-1216, Bangladesh

Assignmento Course Title: Structured Programming Languageo Course Code: CSE 111o Semester: Summer 2016o Program: CSEo 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

Page 2: string , pointer

StringsWHAT 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.

Declaring and 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.

Page 3: string , pointer

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:

Page 4: string , pointer

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:

Page 5: string , pointer

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.

Method Description

strcat() It is used to concatenate(combine) two string

strlen() It is used to calculate length of a string

strrev() It is used to show reverse of a string

strcpy() Copies one string into another

strcmp() It is used to compare two string

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>

Page 6: string , pointer

int main () {

char str1[12] = "BUBT";

char str2[12] = "CSE";

strcat( str1, str2);

printf("strcat( str1, str2): %s\n", str1 );

return 0;

}

Output:

Strcpy () Function:

Page 7: string , pointer

#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) : %s\n", str3 );

return 0;

}

Output:

strlen () Function:

#include <stdio.h>

Page 8: string , pointer

#include <string.h>

int main () {

char str1[12] = "Hello";

char str2[12] = "World";

int len ;

len = strlen(str1);

printf("strlen(str1) : %d\n", len );

return 0;

}

Output:

strcmp () Function:

#include<stdio.h>

Page 9: string , pointer

#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:

Page 10: string , pointer

#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 strcmpi Function:

Page 11: string , pointer

Searching with string:

Page 12: string , pointer

#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++;

Page 13: string , pointer

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

Page 14: string , pointer

#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("\nSorting\n");

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++)

Page 15: string , pointer

printf("%s\t",word[i]);

return 0;

}

Output:

Page 16: string , pointer

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.

Page 17: string , pointer

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

Page 18: string , pointer

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.

Page 19: string , pointer

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 : ");elseprintf("not prime");}

return 0;}

Page 20: string , pointer

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

Page 21: string , pointer

ptr->price = 500;

}