Advertisement
dinophanhk

[C] Get firstName and lastName from fullName

Jun 18th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. // Write C program that converts Full name into initial
  2. // Code by Dino Phan
  3. // http://facebook.com/dinophanhk
  4. //Code programming that I write in Xcode on OSX, some programming tools on Windows will be incompatible.
  5. //Please read instructions on how to edit specific.
  6.  
  7.  
  8. #include <stdio.h>
  9. #include "string.h"
  10.  
  11. void getFirstName(char *fullName, char *firstName);
  12. void getLastName(char *fullName, char *lastName);
  13.  
  14.  
  15. int main()
  16. {
  17.     char cName[30], cFirstName[30], cLastName[30];
  18.    
  19.     printf("Enter your name: ");
  20.     gets(cName);
  21.    
  22.     getFirstName(cName, cFirstName);
  23.     getLastName(cName, cLastName);
  24.    
  25.     printf("%s %s!\n", cFirstName, cLastName);
  26.    
  27. }
  28.  
  29. void getFirstName(char *fullName, char *firstName) {
  30.     int i = 0, j;
  31.     while (fullName[i] != 32) {
  32.         if (fullName[i] == '\0') {
  33.             break;
  34.         }
  35.         i++;
  36.     }
  37.    
  38.     for (j = 0; j < i; j++) {
  39.         firstName[j] = fullName[j];
  40.     }
  41.     firstName[i] = '\0';
  42. }
  43.  
  44. void getLastName(char *fullName, char *lastName) {
  45.     int i, j, k = 0, iLength;
  46.     iLength = (int) strlen(fullName);
  47.     i = iLength;
  48.    
  49.     while (fullName[i - 1] != 32) {
  50.         if (i == 0) {
  51.             break;
  52.         }
  53.         i--;
  54.     }
  55.    
  56.     for (j = i; j < iLength; j++) {
  57.         lastName[k] = fullName[j];
  58.         k++;
  59.     }
  60.     lastName[k] = '\0';
  61. }
  62.  
  63.  
  64. /*
  65. Algorithm description:
  66.  
  67. // Create last name
  68.  1. If fullname include 32th ASCII:
  69.  P h a n   H u y n h     G  i  a     B  a  o \0
  70.  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  71.  
  72.  iLength = 18
  73.  i = 18
  74.  
  75.  -> i = 15
  76.  i = 15 -> k = 0        -> B
  77.  i = 16 -> k = 1        -> a
  78.  i = 17 -> k = 2        -> 0
  79.  break  -> k = 3 = '\0' -> \0
  80.  
  81.  2. If fullname haven't 32th ASCII:
  82.  B a o \0
  83.  0 1 2  3
  84.  
  85.  iLength = 3
  86.  i = 3
  87.  
  88.  -> i = 0
  89.  i = 0 -> k = 0         -> B
  90.  i = 1 -> k = 1         -> a
  91.  i = 2 -> k = 2         -> 0
  92.  break -> k = 3 = '\0'  -> \0
  93.  
  94. // Create first name
  95.  1. If fullname include 32th ASCII:
  96.  P h a n   H u y n h     G  i  a     B  a  o \0
  97.  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  98.  
  99.  i = 0
  100.  
  101.  -> i = 4
  102.  
  103.  for j = 0 -> 3
  104.  j = 0 -> k = 0         -> P
  105.  j = 1 -> k = 1         -> h
  106.  j = 2 -> k = 2         -> a
  107.  j = 3 -> k = 3         -> n
  108.  break -> k = 4         -> \0
  109.  
  110.  2. If fullname haven't 32th ASCII:
  111.  P h a n \0
  112.  0 1 2 3  4
  113.  
  114.  i = 0
  115.  
  116.  -> i = 4
  117.  
  118.  for j = 0 -> 3
  119.  j = 0 -> k = 0         -> P
  120.  j = 1 -> k = 1         -> h
  121.  j = 2 -> k = 2         -> a
  122.  j = 3 -> k = 3         -> n
  123.  break -> k = 4         -> \0
  124.  
  125. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement