Looking for a Tutor Near You?

Post Learning Requirement ยป
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

Strings In C

Loading...

17,597 Views

Strings in C--Their Handling and Common Functions.

Ketan / Gurgaon

7 years of teaching experience

Qualification: 12th (cbse),btech comp.sci.(undergoing)

Teaches: All Subjects, Computer, English, EVS, Mathematics, Social Studies, Chemistry, History, Counting Skills, Reading Skills, Writing Skills

Contact this Tutor
  1. Strings A special kind of array is an array of characters ending in the null character called string arrays A string is declared as an array of characters char s[1 0] char p[30] When declaring a string don't forget to leave a space for the null character which is also known as the string terminator character
  2. C offers four main operations on strings strcpy - copy one string into another strcat - append one string onto the right side of the other strcmp โ€” compare alphabetic order of two strings strlen โ€” return the length of a string
  3. strcpy strcpy(destinationstring, sourcestring) Copies sourcestring into destinationstring For example strcpy(str, "hello world"); assigns "hello world" to the string str
  4. Example with strcpy #include #include main() char x[] = "Example with strcpy", char y[25]; printf("The string in array x is %s \n strcpy(y,x) ; printf("The string in array y is %s \n x);
  5. strcat strcat(destinationstring, sourcestring) appends sourcestring to right hand side of destinationstring For example if str had value "a big strcat(str, "hello world"); appends "hello world" to the string "a big " to get a big hello world"
  6. Example with strcat #include #include main() char x[] = "Example with strcat"; char y[]= "which stands for string concatenation" printf("The string in array x is %s \n ", x); strcat(x,y) ; printf("The string in array x is %s \n ", x);
  7. strcmp strcmp(stringa, stringb) Compares stringa and stringb alphabetically Returns a negative value if stringa precedes stringb alphabetically Returns a positive value if stringb precedes stringa alphabetically Returns 0 if they are equal Note lowercase characters are greater than Uppercase
  8. Example with strcmp #include #include main() char = "cat", char "cat", char "dog", if (strcmp(x,y) 0) printf("The string in array x %s is equal to that in %s \n ", x,y);
  9. continued if (strcmp(x,z) 0) {printf("The string in array x %s is not equal to that in z %s \n x,z); if (strcmp(x,z) < 0) printf("The string in array x %s precedes that in z %s \n else printf("The string in array z %s precedes that in x %s \n else printf( "they are equal"); x,z); z,x);
  10. strlen strlen(str) returns length of string excluding null character strlen("tttt") = 4 not 5 since not counted
  11. Example with strlen #include #include main() int i, count; char x[] = "tommy tucket took a tiny ticket " count = 0; for (i = 0; i < strlen(x);i++) if (x[l] -= printf("The number of t's in %s is %d \n x,count);
  12. Vowels Example with strlen #include #include main() int i, count; char x[] = "tommy tucket took a tiny ticket " count = 0; for (i = 0; i < strlen(x);i++) if ((x[i] count+4-; printf("The number of vowels's in %s is %d \n ", x,count);
  13. No of Words Example with strlen #include #include main() int i, count; char x[] = "tommy tucket took a tiny ticket " count = 0; for (i = 0; i < strlen(x);i++) if ((x[i] COUnt++; printf("The number of words's in %s is %d \n x,count+l);
  14. No of Words Example with more than one space between words #include #include main() int i,j, count; char x[] = "tommy tucket took a tiny ticket " count = 0; for (i = 0; i < strlen(x);i++) if ((x[i] { count++; printf("The number of words's in %s is %d \n x,count+l);
  15. Input output functions of characters and strings getchar() reads a character from the screen in a non-interactive environment getche() like getchar() except interactive putchar(int ch) outputs a character to screen gets(str) gets a string from the keyboard puts(str) outputs string to screen
  16. Characters are at the heart Of strings
  17. Exercise 1 Output 1 12 123 12 3 4 12345678910
  18. Exercise 1 #include main() int i,j; for(J = for(i=l ;i j;i++) printf("%d " , i); printf("\n");
  19. Indlno 9S!019)(3
  20. Exercise 2 #include main() int i,j; for(J = for(i=l ;i j;i++) printf( printf("\n");
  21. Indlno C 9S!0419X3
  22. #include main() int i,j; for(j = 1; j 10; j++) printf( for(i=l ;i 8;i++) if ((j==l) Il printf( else printf(" printf("* \n ");
  23. Some Useful C Character Functions Don't forget to #include to get the function prototypes.
  24. Functions Function int isalpha(c); int isupper(c); int islower(c); int isdigit(c); Return true if c is a letter. c is an upper case letter. c is a lower case letter. c is a digit [0-9].
  25. Function More Functions Return true if int isxdigit(c); cis a hexadecimal digit [O-9A-Fa-f]. int isalnum(c); c is an alphanumeric character (c is a letter or a digit); , int isspace(c); c is a SPACE, TAB, RETURN, NEWLINE, FORMFEED, or vertical tab character.
  26. Even More C Functions Function int ispunct(c); int isprint(c); int iscntrl(c); Return true if c is a punctuation character (neither control nor alphanumeric). c is a printing character. c is a delete character or ordinary control character.
  27. Still More C Functions Function int isascii(c); int toupper(int c); int tolower(int c); Return true if c is an ASCII character, codeless than 0200. convert character c to upper case (leave it alone if not lower) convert character c to lower (leave it alone if not upper)
  28. Program to Reverse Strings #include #include int main () int i; char all 0]; char temp; //clrscr(); // only works on windows gets(a) ; for (i = 0; a[i] '\0' ; i++); for (int j = 0; j i/2 ; j++) temp = a[j]; ai - j] = temp; printf("%s",a); return (0);
  29. Program to count the number of vowels in a string . Note Two different ways to declare strings One using pointers *str Two using character array char a[] #include #include void main() { char *str; char a[]="aeiouAElOU"; int i,j,count=0; clrscr() ; printf("\nEnter the string\n"); gets(str); if(a[J] = = str[i] count++; break; printf("\nNo. of vowels = %d",count);