functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case...

39
Functions: call by reference vs. call by value void change(int x[ ], int s){ x[0] = 50; s = 7; } void main(){ int n = 3; int a[ ] = {60,70,80}; change(a, n); printf(“%d %d”, a[0], n); } Prints 50 3

Upload: others

Post on 03-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Functions: call by reference vs.call by value

void change(int x[ ], int s){

x[0] = 50;s = 7;

}void main(){void main(){

int n = 3;int a[ ] = {60,70,80};

change(a, n);printf(“%d %d”, a[0], n);

}

Prints 50 3

Page 2: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Strings

• A special kind of array – an array of characters ending with the null character

‘\0’ called string

• A string is declared as an array of characters• char s[10]• char s[10]• char p[30]

• When declaring a string don’t forget to leave a space for the null character which is also known as the string terminator character

Page 3: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Strings input output

• gets(s) – take a string as input and place it in array s

• puts(s) – show the content of the string s

#include <stdio.h> #include <string.h> #include <string.h> int main() {

char s[30]; printf("Please enter a sentence: ");gets(s);puts("You have entered: ");puts(s); return 0;

}

Page 4: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Strings initialization at the time of declartion

#include <stdio.h> #include <string.h> int main() { int main() {

char s[80]="To be or not to be that is the question"; puts(s); return 0;

}

Page 5: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

C offers following major library functions on strings

• strlen(s) – return the length of a string s

• strlwr(s)– convert the string s in lower case

• strupr(s) – convert the string s in upper case

• strrev(s) – reverse the content of the string s

• strcpy(s, t) – copy string t into another string s• strcpy(s, t) – copy string t into another string s

• strncpy(s, t, n) - copy n characters of string t into another string s

• strcat(s, t) – append string t into the right side of the string s

• strncat(s, t, n) - append n characters of the string t onto the right side of the string s

• strcmp(s, t) – compare alphabetic order of two strings s and t

Page 6: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strlen(s) – returns the length of a string s

Page 7: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main( ) {

char str[20] = "BeginnersBook"; int length;length = strlen(str);length = strlen(str);printf("Length of the string is : %d", length); return 0;

}

Length of the string is: 13

Output:

Page 8: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strlwr(s)– convert the string s in lower case

Page 9: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main( ) {

char str[20] = "BeginnersBook"; strlwr(str);printf("%s",str); printf("%s",str); return 0;

}

beginnersbook

Output:

Page 10: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strupr(s)– convert the string s in upper case

Page 11: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main( ) {

char str[20] = "BeginnersBook"; strupr(str);printf("%s",str); printf("%s",str); return 0;

}

BEGINNERSBOOK

Output:

Page 12: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strrev(s) – reverse the content of the string s

Page 13: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main( ) {

char str[20] = "DRAWER"; strrev(str);printf("%s",str); printf("%s",str); return 0;

}

REWARD

Output:

Page 14: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strcpy(s, t) – copy string t into another string s

Page 15: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[30] = "Bad"; char s2[30] = "Good"; strcpy(s1, s2); strcpy(s1, s2); printf("%s",s1); return 0;

}

Good

Output:

Page 16: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[30] = "Bad"; char s2[30] = "Good"; strcpy(s2, s1); strcpy(s2, s1); printf("%s",s2); return 0;

}

Bad

Output:

Page 17: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strncpy(s, t, n) - copy n characters of string t into another string s

Page 18: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[30] = "Coastal"; char s2[30] = "Cry"; strncpy(s1, s2,3); strncpy(s1, s2,3); printf("%s",s1); return 0;

}

Crystal

Output:

Page 19: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[30] = "Coastal"; char s2[30] = "Cry"; strncpy(s1, s2,4); strncpy(s1, s2,4); printf("%s",s1); return 0;

}

Cry

Output:

Page 20: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strcat(s, t) – append string t into the right side of the string s

Page 21: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[30] = "Hello "; char s2[30] = "World"; strcat(s1, s2); strcat(s1, s2); printf("%s",s1); return 0;

}

Hello World

Output:

Page 22: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strncat(s, t, n) - append n characters of the string t onto the right side of the string s

Page 23: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[30] = ""; char s2[30] ="Happy "; strncat(s1, s2, 6); strncat(s1, s2, 6); printf("%s",s1); return 0;

}

Happy

Output:

Page 24: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

LITTLE QUIZ FOR YOU

Page 25: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[30] = "Happy "; char s2[30] = "New Year!"; char s3[30] = ""; strcat(s1, s2); s1 = "Happy New Year!"strcat(s1, s2); strncat(s3, s1,6);strcat(s3, s1); printf("%s",s3); return 0;

}

Happy Happy New Year!

Output:

s1 = "Happy New Year!"

s3 = "Happy "

s3 = "Happy Happy New Year!"

Page 26: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strcmp(s, t) – compare alphabetic order of two strings s and t

Page 27: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strcmp

• strcmp(s, t)

• Compares s and t alphabetically

• Returns a negative value if s precedes t alphabetically

• Returns a negative value if s precedes t alphabetically

• Returns a positive value if t precedes s alphabetically

• Returns 0 if they are same

• Note lowercase characters are greater than Uppercase

Page 28: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[ ] = "cat"; char s2[] = "cat"; char s3[] = "dog"; int x = strcmp(s1, s2); if(x == 0)

printf("They are same") ; printf("They are same") ; else if (x < 0)

printf("s1 comes before s2") ; else if (x > 0)

printf("s1 comes after s2") ; return 0;

}

They are same

Output:

Page 29: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[ ] = "cat"; char s2[] = "cat"; char s3[] = "dog"; int x = strcmp(s1, s3);if(x == 0)

printf("They are same") ; printf("They are same") ; else if (x < 0)

printf("s1 comes before s3") ; else if (x > 0)

printf("s1 comes after s3") ; return 0;

}

s1 comes before s3

Output:

Page 30: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strcmpi(s, t) – compare alphabetic order of two strings s and t ignoring case

Page 31: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[ ] = "cat"; char s2[] = “Cat"; char s3[] = "dog"; int x = strcmp(s1, s2) ;if(x == 0)

printf("They are same") ; printf("They are same") ; else if (x< 0)

printf("s1 comes before s2") ; else if (x > 0)

printf("s1 comes after s2") ; return 0;

}

s1 comes after s2

Output:

Page 32: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example

#include <stdio.h> #include <string.h> int main() {

char s1[ ] = "cat"; char s2[] = “Cat"; char s3[] = "dog";int x = strcmpi(s1, s2); if(x == 0)

printf("They are same") ; printf("They are same") ; else if (x < 0)

printf("s1 comes before s2") ; else if (x > 0)

printf("s1 comes after s2") ; return 0;

}

They are same

Output:

Page 33: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Program: Palindrome testing

Page 34: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example: Palindrome testing

#include <stdio.h> #include <string.h> int main() {

char s[80]="madam";char t[80];

gets(s);strcpy(t,s);strcpy(t,s);strrev(t);if(strcmpi(s,t) == 0)

printf("\"%s\" is a palindrom", s);else

printf("\"%s\" is NOT a palindrom", s); return 0;

}

Page 35: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

strlen

• strlen(str) returns length of string excluding null character

• strlen(“tttt”) = 4 not 5 since \0 not counted

Page 36: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Example with strlen

#include <stdio.h>#include <string.h>

main(){ int i, count;char x[] = “tommy tucket took a tiny ticket ”;count = 0;

for (i = 0; i < strlen(x);i++){ if (x[i] == ‘t’) count++;}

printf(“The number of t’s in %s is %d \n “, x,count);

}

Page 37: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

Vowels Example with strlen

#include <stdio.h>#include <string.h>

main(){ int i, count;char x[] = “tommy tucket took a tiny ticket ”;count = 0;

for (i = 0; i < strlen(x);i++){ if ((x[i] == ‘a’)||(x[i]==‘e’)||(x[i]==‘I’)||(x[i]==‘o’)||(x[i]==‘u’))

count++;}

printf(“The number of vowels’s in %s is %d \n “, x,count);

}

Page 38: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

No of Words Example with strlen

#include <stdio.h>#include <string.h>

main(){ int i, count;char x[ ] = “tommy tucket took a tiny ticket ”;count = 0;

for (i = 0; i < strlen(x);i++){ if ((x[i] == ‘ ‘) count++;}

printf(“The number of words’s in %s is %d \n “, x,count+1);

}

Page 39: Functions:call by reference vs. call by value · • strlwr(s)–convert the string s in lower case • strupr(s) –convert the string s in upper case • strrev(s) –reverse the

No of Words Example with more than one space between words

#include <stdio.h>#include <string.h>

main(){

int i,j, count;char x[] = “tommy tucket took a tiny ticket ”;

count = 0;

for (i = 0; i < strlen(x);i++)for (i = 0; i < strlen(x);i++){

if ((x[i] == ‘ ‘) { count++;for(j=i;x[j] != ‘ ‘;j++);i = j;

}}

printf(“The number of words’s in %s is %d \n “, x,count+1);

}