Lecture
ON
Applications of Strings & Pointers
V.Radhesyam
Assistant professor
Department of IT:VRSEC
Radhesyam.V
Array of Strings
1. #include <stdio.h>
2. int main()
3. {
4. char charr[7][10] = {"sun","mon","tue","wed","thu","fri","sat"};
5. int i;
6. for(i=0;i<7;i++)
7. printf("%sn",charr[i]);
8. return 0;
9. }
2
Output:
sun
mon
tue
wed
thu
fri
sat
Radhesyam.V
Array of Strings
1. #include <stdio.h>
2. Void main()
3. {
4. char charr[7][10];
5. int i;
6. printf("enter string");
7. for(i=0;i<7;i++)
8. scanf("%s",charr[i]);
9. printf("string is ");
10. for(i=0;i<7;i++)
11. printf("%sn",charr[i]);
12. }
3
Maximum no of strings
Maximum string length
Radhesyam.V
Pointer arithmetic
4
1. void main()
2. {
3. int first, second, *p, *q, sum,sub,mul,di;
4. printf("Enter two integers n");
5. scanf("%d%d", &first, &second);
6. p = &first;
7. q = &second;
8. sum = *p + *q;
9. sub = *p - *q;
10.mul = (*p) * (*q);
11.di = (*p) /(*q);
12.printf("Sum of the numbers = %dn", sum);
13.printf("Sub of the numbers = %dn", sub);
14.printf("mul of the numbers = %dn", mul);
15.printf("div of the numbers = %dn", di);
16.}
Radhesyam.V
Double Pointer
5
Radhesyam.V
Double pointer Program
6
1. Void main()
2. {
3. int var = 120;
4. int *varptr = &var;
5. int **doubleptr = &varptr;
6. printf("Value of var = %dn", var );
7. printf("Value of var pointer = %dn", *varptr );
8. printf("Value of double pointer = %dn", **doubleptr);
9. printf("Address of var = %pn", &var );
10. printf("Address of var pointer = %pn", &varptr );
11. printf("Value in var pointer = %pn", varptr );
12. printf("Address of double pointer = %pn", *doubleptr);
13. printf("Value in double pointer = %pn", doubleptr);
14. }
Output:
Value of var = 120
Value of var pointer = 120
Value of double pointer = 120
Address of var = 0x7ffe25192bfc
Address of var pointer = 0x7ffe25192c00
Value in var pointer = 0x7ffe25192bfc
Address of double pointer = 0x7ffe25192bfc
Value in double pointer = 0x7ffe25192c00
Radhesyam.V
Scanf vs gets
7
Scanf() Gets()
scanf() function can read different
data types.
get() function will only get character
string data.
scanf() function takes the format
string and list of addresses of
variables. e.g. scanf(“%d”,
&number);
get() function takes the name of the
variable to store the received value.
e.g. gets(name);
Read multiple values One string
Scanf () reads input until it
encounters whitespace, newline or
End Of File (EOF)
gets () reads input until it encounters
newline or End Of File (EOF)
Radhesyam.V
Fibonacci Series Using Recursion
1. int fib(int n)
2. {
3. if (n <= 1)
4. return n;
5. return fib(n-1) + fib(n-2);
6. }
7. int main ()
8. {
9. int n = 9;
10. printf("%d", fib(n));
11. getchar();
12. return 0;
13. }
8
Radhesyam.V
Ceil()
#include <stdio.h>
#include <math.h>
int main()
{
double num = 8.33;
int result;
result = ceil(num);
printf("Ceiling integer of %.2f = %d", num, result);
return 0;
}
9
Output:
Ceiling integer of 8.33 = 9
Radhesyam.V
Floor()
#include <stdio.h>
#include <math.h>
int main()
{ double num = -8.33;
int result;
result = floor(num);
printf("Floor integer of %.2f = %d", num, result);
return 0;
}
10
Output:
Floor integer of -8.33 = -9

Array strings

  • 1.
    Lecture ON Applications of Strings& Pointers V.Radhesyam Assistant professor Department of IT:VRSEC
  • 2.
    Radhesyam.V Array of Strings 1.#include <stdio.h> 2. int main() 3. { 4. char charr[7][10] = {"sun","mon","tue","wed","thu","fri","sat"}; 5. int i; 6. for(i=0;i<7;i++) 7. printf("%sn",charr[i]); 8. return 0; 9. } 2 Output: sun mon tue wed thu fri sat
  • 3.
    Radhesyam.V Array of Strings 1.#include <stdio.h> 2. Void main() 3. { 4. char charr[7][10]; 5. int i; 6. printf("enter string"); 7. for(i=0;i<7;i++) 8. scanf("%s",charr[i]); 9. printf("string is "); 10. for(i=0;i<7;i++) 11. printf("%sn",charr[i]); 12. } 3 Maximum no of strings Maximum string length
  • 4.
    Radhesyam.V Pointer arithmetic 4 1. voidmain() 2. { 3. int first, second, *p, *q, sum,sub,mul,di; 4. printf("Enter two integers n"); 5. scanf("%d%d", &first, &second); 6. p = &first; 7. q = &second; 8. sum = *p + *q; 9. sub = *p - *q; 10.mul = (*p) * (*q); 11.di = (*p) /(*q); 12.printf("Sum of the numbers = %dn", sum); 13.printf("Sub of the numbers = %dn", sub); 14.printf("mul of the numbers = %dn", mul); 15.printf("div of the numbers = %dn", di); 16.}
  • 5.
  • 6.
    Radhesyam.V Double pointer Program 6 1.Void main() 2. { 3. int var = 120; 4. int *varptr = &var; 5. int **doubleptr = &varptr; 6. printf("Value of var = %dn", var ); 7. printf("Value of var pointer = %dn", *varptr ); 8. printf("Value of double pointer = %dn", **doubleptr); 9. printf("Address of var = %pn", &var ); 10. printf("Address of var pointer = %pn", &varptr ); 11. printf("Value in var pointer = %pn", varptr ); 12. printf("Address of double pointer = %pn", *doubleptr); 13. printf("Value in double pointer = %pn", doubleptr); 14. } Output: Value of var = 120 Value of var pointer = 120 Value of double pointer = 120 Address of var = 0x7ffe25192bfc Address of var pointer = 0x7ffe25192c00 Value in var pointer = 0x7ffe25192bfc Address of double pointer = 0x7ffe25192bfc Value in double pointer = 0x7ffe25192c00
  • 7.
    Radhesyam.V Scanf vs gets 7 Scanf()Gets() scanf() function can read different data types. get() function will only get character string data. scanf() function takes the format string and list of addresses of variables. e.g. scanf(“%d”, &number); get() function takes the name of the variable to store the received value. e.g. gets(name); Read multiple values One string Scanf () reads input until it encounters whitespace, newline or End Of File (EOF) gets () reads input until it encounters newline or End Of File (EOF)
  • 8.
    Radhesyam.V Fibonacci Series UsingRecursion 1. int fib(int n) 2. { 3. if (n <= 1) 4. return n; 5. return fib(n-1) + fib(n-2); 6. } 7. int main () 8. { 9. int n = 9; 10. printf("%d", fib(n)); 11. getchar(); 12. return 0; 13. } 8
  • 9.
    Radhesyam.V Ceil() #include <stdio.h> #include <math.h> intmain() { double num = 8.33; int result; result = ceil(num); printf("Ceiling integer of %.2f = %d", num, result); return 0; } 9 Output: Ceiling integer of 8.33 = 9
  • 10.
    Radhesyam.V Floor() #include <stdio.h> #include <math.h> intmain() { double num = -8.33; int result; result = floor(num); printf("Floor integer of %.2f = %d", num, result); return 0; } 10 Output: Floor integer of -8.33 = -9