Lab 12 - PowerPoint PPT Presentation

About This Presentation
Title:

Lab 12

Description:

String input is done using scanf with %s descriptor for strings seperated by ... Write a C program taht reads a string and displays it in reverse order. ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 11
Provided by: davidls4
Category:
Tags: lab | taht

less

Transcript and Presenter's Notes

Title: Lab 12


1
Lab 12
  • Strings
  • Recursion
  • Note
  • Read the whole Chapter 9 and 10.

2
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.
  • includeltstdio.hgt
  • void main()
  • char first_name20,last_name20,city25
  • 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_na
    me)
  • puts(city)

3
  • //using getchar() and putchar
  • includeltstdio.hgt
  • void main()
  • char c
  • printf("Enter a character")
  • c getchar()
  • putchar(c)
  • //using strcat
  • includeltstdio.hgt
  • include ltstring.hgt
  • void main(void)
  • char ch115"hello"
  • char ch2" Sir"
  • printf("Before s\n" ,ch1)
  • strcat(ch1,ch2)
  • printf("After s\n" ,ch1)

4
  • //using strncat
  • includeltstdio.hgt
  • include ltstring.hgt
  • void main(void)
  • char ch115"hello"
  • char ch2" Sir"
  • printf("Before s\n" ,ch1)
  • strncat(ch1,ch2,2)
  • printf("After s\n" ,ch1)
  • //using strlen()
  • includeltstdio.hgt
  • include ltstring.hgt
  • void main(void)
  • char ch115"hello"
  • char ch2" Sir"
  • printf("Before s\n" ,ch1)
  • strncat(ch1,ch2,2)
  • printf("After s, ch1 size d \n"
    ,ch1,strlen(ch1))

5
  • //using strcmp
  • includeltstdio.hgt
  • include ltstring.hgt
  • void main(void)
  • char ch115"Amal"
  • char ch2"Yasmine"
  • int result
  • resultstrcmp(ch1,ch2)
  • if(result gt0)
  • printf("ch1 after ch2 \n")
  • else if(result lt0)
  • printf("ch1 before ch2 \n")
  • else
  • printf("the 2 strings are equal \n")

6
  • Exercise1
  • use strncmp(string1,string2,lgmax)
  • use strnicmp(string1,string2,lgmax)
  • includeltstdio.hgt
  • include ltstring.hgt
  • void main(void)
  • char ch115"AM"
  • char ch2"am"
  • int result
  • resultstricmp(ch1,ch2)
  • if(result gt0)
  • printf("ch1 after ch2 \n")
  • else if(result lt0)
  • printf("ch1 before ch2 \n")
  • else
  • printf("the 2 strings are equal \n")

7
  • //use of strncpy
  • includeltstdio.hgt
  • include ltstring.hgt
  • void main(void)
  • char ch115"AAAAAAAAAAAAA"
  • char ch2"BBBBB"
  • strncpy(ch1,ch2,5)
  • printf("ch1s\n",ch1)
  • //use of atoi
  • includeltstdio.hgt
  • include ltstdlib.hgt
  • void main(void)
  • char ch115"143"
  • printf("positiond\n",atoi(ch1))

8
  • //use of atof()
  • includeltstdio.hgt
  • include ltstdlib.hgt
  • void main(void)
  • char ch115"143.4"
  • printf("position.2f\n",atof(ch1))
  • //use of strchr
  • includeltstdio.hgt
  • include ltstring.hgt
  • efine CAR 'e'
  • define LGMAX 132
  • void main(void)
  • char ch1LGMAX1
  • char adr
  • int ncar
  • printf("Enter a string ")
  • gets(ch1)
  • ncar0
  • adrch1

9
  • 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
  • includeltstdio.hgt
  • long facto(int n)
  • long result
  • if(ngt1)
  • resultfacto(n-1)n
  • else
  • result1
  • return result
  • void main(void)
  • int n
  • printf("enter n ")
  • scanf("d",n)
  • printf("Factorial of d is ld \n",n,facto(n))

10
  • 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 mgt0 and n gt0
  • A(0,n)n1 for ngt0
  • A(m,0)A(m-1,1) for mgt 0
Write a Comment
User Comments (0)
About PowerShow.com