1 / 7

Today’s Agenda

Today’s Agenda . Strings Two dimensional Arrays Matrix. String Manipulation functions in string.h. strcpy(s1,s2) /* copies s2 into s1 */ strcat(s1,s2) /* concatenates s2 to s1 */ strlen(s) /* returns the length of s */ strcmp(s1,s2) /*returns 0 if s1 and s2 are same

chelsey
Download Presentation

Today’s Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Today’s Agenda • Strings • Two dimensional Arrays • Matrix

  2. String Manipulation functions in string.h • strcpy(s1,s2) /* copies s2 into s1 */ • strcat(s1,s2) /* concatenates s2 to s1 */ • strlen(s) /* returns the length of s */ • strcmp(s1,s2)/*returns 0 if s1 and s2 are same • returns less then 0 if s1<s2 • returns greater than 0 if s1>s2 */

  3. String length • char txt[100],c; • int i=0,len=0; • gets(txt); • while(txt[i] != '\0') • { • i++; • } • printf("len = %d\n",i); • printf("len using strlen is %d\n",strlen(txt));

  4. Implementation of strcat() • void main() /* s2 is concatenated after s1 */ • { • char s1[100],s2[100]; • int i = 0,j = 0; • printf("Enter first string\n"); • scanf("%[^\n]",s1); • printf("Enter second string\n"); • scanf("%[^\n]",s2); • while(s1[i++] != '\0'); • i--; • while(s2[j] != '\0') • s1[i++] = s2[j++]; • s1[i] = '\0'; • printf("\n Final string is:%s",s1); • }

  5. Important Character functions in ctype.h • isdigit(c) /*Returns a nonzero if c is a digit*/ • islower(c) /* Returns a nonzero if c is a lower case • alphabetic character */ • isalpha(c) /*Returns a nonzero if c is an alphabet*/ • isspace(c) /*Returns a nonzero for blanks */ • isupper(c) /*Returns a nonzero if c is capital letter*/ • toupper(c) /* Returns upper case of c */ • tolower(c)/* Returns lower case of c */

  6. 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; for(left = 0,right = len-1; 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"); }

  7. Write a program to search a user entered word in a user entered sentence and also find the number of times it is occurring.

More Related