Advertisement
Guest User

Untitled

a guest
Nov 1st, 2011
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. /*
  2. doodles, ignore the no-adherence to style-mashtyle
  3. strings
  4. some more
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. int main() {
  12.     char mySentence[50]; /*can initialize a string when declared*/
  13.     char yetAnotherSentence[50];
  14.     char concatedSentence[50];
  15.  
  16.     int lenOfMySent = 0, lenOfYetASent = 0, lenOfConcatSent = 0;
  17.  
  18.     printf("///////////////////////////////////////////////////////////////////\n");
  19.     printf("\nEnter a sentence(<=49 in size)\n to read into mySentence using fgets :");
  20.     fgets(mySentence,49,stdin);
  21.     printf("The value in mySentence is %s", mySentence);
  22.     printf("The text in mySentence with value from user with the newline is \n[%s]\n", mySentence);
  23.  
  24.     lenOfMySent = strlen(mySentence); /* using the strlen in string.h library to get the length of the mySentence
  25.                                       includes the newline that the user entered to indicate done entering the string
  26.                                       but does not include the string delimitor
  27.                                       */
  28.  
  29.     mySentence[lenOfMySent - 1] = '\0'; /*manually get rid of the newline that the user entered to indicate done entering the string*/
  30.     printf("The text in mySentence now that we've taken away the end newline is\n [%s]\n", mySentence);
  31.     printf("The length of mySentence now that we've taken away the end newline is [%d]\n", lenOfMySent);
  32.     /////////////////////////////////////////////////////////////////////////////////////////////////////////
  33.     printf("///////////////////////////////////////////////////////////////////\n");
  34.     printf("\nEnter a sentence(<=49 in size)\n to read into yetAnotherSentence using fgets :");
  35.     fgets(yetAnotherSentence,49,stdin);
  36.     printf("The value in yetAnotherSentence is %s", yetAnotherSentence);
  37.     printf("The text in mySentence with value from user with the newline is \n[%s]\n", yetAnotherSentence);
  38.  
  39.     lenOfYetASent = strlen(yetAnotherSentence); /* using the strlen in string.h library to get the length of the mySentence
  40.                                                 includes the newline that the user entered to indicate done entering the string
  41.                                                 but does not include the string delimitor
  42.                                                 */
  43.  
  44.     yetAnotherSentence[lenOfYetASent - 1] = '\0'; /*manually get rid of the newline that the user entered to indicate done entering the string*/
  45.     printf("The text in yetAnotherSentence now that we've taken away the end newline is\n [%s]\n", yetAnotherSentence);
  46.     printf("The length of yetAnotherSentence now that we've taken away the end newline is [%d]\n", lenOfYetASent);
  47.     printf("///////////////////////////////////////////////////////////////////\n");
  48.     /////////////////////////////////////////////////////////////////////////////////////////////////////////
  49.     /*************************** Copy first 1/2 of mySentence onto concatedSentence  ****************************************/
  50.     strncpy(concatedSentence, mySentence, lenOfMySent / 2); /* read on strncpy*/
  51.     /* lenOfMySent / 2  = 1/2 the lemgth of the first string mySentence */
  52.     /*
  53.     Now concatedSentence has the first 1/2 of the first string mySentence
  54.     BUT
  55.     is not terminated by the string terminating character '\0' which tells the
  56.     compiler that a string has ended
  57.     so is not a legit string as yet
  58.     */
  59.     concatedSentence[lenOfMySent / 2] = '\0'; /* append the null ('\0') at the end of the string to make it legal*/
  60.     printf("The text in concatedSentence with the first 1/2 of first string is [%s]\n", concatedSentence);
  61.     /*************************** Copy second 1/2 of yetAnotherSentence onto concatedSentence  ****************************************/
  62.  
  63.     system("pause");
  64.     return 0;  
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement