100 likes | 232 Vues
Lab 12. Strings Recursion Note: Read the whole Chapter 9 and 10. Review of Strings. Strings in C are arrays of characters ended by the null character ‘’.
E N D
Lab 12 • Strings • Recursion Note: Read the whole Chapter 9 and 10.
Review of Strings • Strings in C are arrays of characters ended by the null character ‘\0’. • String input is done using scanf with %s descriptor for strings seperated by whitespace, usings gets for input of whole lines, and using getchar for single character input. • String output is done using printf with %s descriptor; putchar does single character output. # include<stdio.h> void main(){ char first_name[20],last_name[20],city[25]; printf("Enter the city name:"); gets(city); printf("Enter your last and first name :"); scanf("%s %s",first_name,last_name); printf("Hi %s %s, you live in ",first_name,last_name); puts(city);}
//using getchar() and putchar # include<stdio.h> void main(){ char c; printf("Enter a character:"); c= getchar(); putchar(c);} //using strcat # include<stdio.h> # include <string.h> void main(void) { char ch1[15]="hello"; char *ch2=" Sir"; printf("Before %s\n" ,ch1); strcat(ch1,ch2); printf("After %s\n" ,ch1); }
//using strncat # include<stdio.h> # include <string.h> void main(void) { char ch1[15]="hello"; char *ch2=" Sir"; printf("Before %s\n" ,ch1); strncat(ch1,ch2,2); printf("After %s\n" ,ch1);} //using strlen() # include<stdio.h> # include <string.h> void main(void) { char ch1[15]="hello"; char *ch2=" Sir"; printf("Before %s\n" ,ch1); strncat(ch1,ch2,2); printf("After %s, ch1 size %d \n" ,ch1,strlen(ch1));}
//using strcmp # include<stdio.h> # include <string.h> void main(void) { char ch1[15]="Amal"; char *ch2="Yasmine"; int result; result=strcmp(ch1,ch2); if(result >0) printf("ch1 after ch2 \n"); else if(result <0) printf("ch1 before ch2 \n"); else printf("the 2 strings are equal \n");}
Exercise1 : • use strncmp(string1,string2,lgmax); • use strnicmp(string1,string2,lgmax) # include<stdio.h> # include <string.h> void main(void) { char ch1[15]="AM"; char *ch2="am"; int result; result=stricmp(ch1,ch2); if(result >0) printf("ch1 after ch2 \n"); else if(result <0) printf("ch1 before ch2 \n"); else printf("the 2 strings are equal \n");}
//use of strncpy # include<stdio.h> # include <string.h> void main(void) { char ch1[15]="AAAAAAAAAAAAA"; char *ch2="BBBBB"; strncpy(ch1,ch2,5); printf("ch1=%s\n",ch1);} //use of atoi # include<stdio.h> # include <stdlib.h> void main(void) { char ch1[15]="143"; printf("position=%d\n",atoi(ch1));}
//use of atof() # include<stdio.h> # include <stdlib.h> void main(void) {char ch1[15]="143.4"; printf("position=%.2f\n",atof(ch1));} //use of strchr # include<stdio.h> # include <string.h> # efine CAR 'e' # define LGMAX 132 void main(void) { char ch1[LGMAX+1]; char *adr; int ncar; printf("Enter a string "); gets(ch1); ncar=0; adr=ch1; while(adr==strchr(adr,CAR)) {ncar++; adr++; } printf(" the number of e is %d",ncar);}
Write a C program that deletes all the letters ‘e’ in a string and displays the new string (use strchr() and strcpy()). • Write a C program taht reads a string and displays it in reverse order. (use strlen() and putchar()). • Recursion # include<stdio.h> long facto(int n) {long result; if(n>1) result=facto(n-1)*n; else result=1; return result;} void main(void) {int n; printf("enter n :"); scanf("%d",&n); printf("Factorial of %d is %ld \n",n,facto(n)); }
Write a C program that use a recursive function to compute « Ackermann function » defined as following : • A(m,n)=A(m-1,A(m,n-1)) for m>0 and n >0 • A(0,n)=n+1 for n>0 • A(m,0)=A(m-1,1) for m> 0