The length of string Portability Defined in ANSI C Example strlen example

The length of string portability defined in ansi c

This preview shows page 3 - 8 out of 14 pages.

The length of string . Portability. Defined in ANSI-C. Example. /* strlen example */ #include <stdio.h> #include <string.h> main () { char szInput[256]; printf ("Enter a sentence: "); gets (szInput); printf ("Sentence entered is %u characters long\n",strlen(szInput)); } Output: Enter sentence: just testing Sentence entered is 12 characters long
Image of page 3
4 strcpy (dest, src ) Copy string. Copies the content pointed by src to dest stopping after the terminating null-character is copied. dest should have enough memory space allocated to contain src string. Parameters. dest Destination string. Should be enough long to contain string2 . string2 Null-terminated string to copy. Return Value. dest is returned. Portability. Defined in ANSI-C. Example. /* strcpy example */ #include <stdio.h> #include <string.h> main () { char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful"); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); } Output: str1: Sample string str2: Sample string str3: copy successful
Image of page 4
5 strncpy (dest, src, num ) Copy characters from one string to another. Copies the first num characters of src to dest . No null-character is implicitly appended to dest after copying process. So dest may not be null- terminated if no null-caracters are copied from src . If num is greater than the length of src , dest is padded with zeros until num . Parameters. dest Destination string. Space allocated should be at least num characters long. string2 Null-terminated string. num Number of characters to be copied. Return Value. dest is returned. Portability. Defined in ANSI-C. Example. /* strncpy example */ #include <stdio.h> #include <string.h> main () { char str1[]= "To be or not to be"; char str2[6]; strncpy (str2,str1,5); str2[5]='\0'; puts (str2); } Output: To be
Image of page 5
6 strcat (dest, src ) Append string. Appends src string to dest string. The terminating null character in dest is overwritten by the first character of src . The resulting string includes a null-character at end. Parameters. dest Pointer to a null-terminated string with enough space allocated to contain both src and dest . src Null-terminated string to append. Return Value. dest is returned. Portability. Defined in ANSI-C. Example. /* strcat example */ #include <stdio.h> #include <string.h> main () { char str[80]; strcpy (str,"strings "); strcat (str,"have been "); strcat (str,"concatenated."); puts (str); } Output: strings have been concatenated.
Image of page 6
7 strncat (dest, src, num ) Append substring to string.
Image of page 7
Image of page 8

You've reached the end of your free preview.

Want to read all 14 pages?

  • Fall '15
  • ASCII, char c

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture