Lec31 - Computer Programming I TA C162. Today's Agenda...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Computer Programming I TA C162 . 5 Apr 2007 Today’s Agenda Character Arrays One dimensional Arrays  Coding Examples Arrays of strings (Two Dimensional Char Arrays) Declaration Initialization Printing Reading Coding Examples
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Computer Programming I TA C162 . 5 Apr 2007 Palindrome problem: Algorithm Design 1. Read a string in str[0….n-1]   pre condition n>=1 2.  Initialize left = 0 and right = n-1 1. Repeat for  left<right         3.1  str[left] != str[right] terminate with false         3.2  Increment left and decrement right 1. Terminate with true  Post condition   Returns false If str[left] != str[right] for any value of left>=0 and right<=n-1  Where left<right
Background image of page 2
Computer Programming I TA C162 . 5 Apr 2007 Palindrome problem: Implementation void main() { char str[80]; int left,right,i,len,flag = 1; printf("Enter a string"); for(i = 0;(str[i] = getchar())!='\n';++i); len = i-1; for(left = 0,right = len; left < right; ++left,--right) { if(str[left]!= str[right]) { flag = 0; break; } } if(flag)printf("\n String is palindrome"); else printf("\n String is not a palindrome"); } Exercise: Write equivalent while loop for the above. flag =1
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

Lec31 - Computer Programming I TA C162. Today's Agenda...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online