Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Write C program that converts Full name into initial
- // Code by Dino Phan
- // http://facebook.com/dinophanhk
- //Code programming that I write in Xcode on OSX, some programming tools on Windows will be incompatible.
- //Please read instructions on how to edit specific.
- #include <stdio.h>
- #include "string.h"
- void getFirstName(char *fullName, char *firstName);
- void getLastName(char *fullName, char *lastName);
- int main()
- {
- char cName[30], cFirstName[30], cLastName[30];
- printf("Enter your name: ");
- gets(cName);
- getFirstName(cName, cFirstName);
- getLastName(cName, cLastName);
- printf("%s %s!\n", cFirstName, cLastName);
- }
- void getFirstName(char *fullName, char *firstName) {
- int i = 0, j;
- while (fullName[i] != 32) {
- if (fullName[i] == '\0') {
- break;
- }
- i++;
- }
- for (j = 0; j < i; j++) {
- firstName[j] = fullName[j];
- }
- firstName[i] = '\0';
- }
- void getLastName(char *fullName, char *lastName) {
- int i, j, k = 0, iLength;
- iLength = (int) strlen(fullName);
- i = iLength;
- while (fullName[i - 1] != 32) {
- if (i == 0) {
- break;
- }
- i--;
- }
- for (j = i; j < iLength; j++) {
- lastName[k] = fullName[j];
- k++;
- }
- lastName[k] = '\0';
- }
- /*
- Algorithm description:
- // Create last name
- 1. If fullname include 32th ASCII:
- P h a n H u y n h G i a B a o \0
- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
- iLength = 18
- i = 18
- -> i = 15
- i = 15 -> k = 0 -> B
- i = 16 -> k = 1 -> a
- i = 17 -> k = 2 -> 0
- break -> k = 3 = '\0' -> \0
- 2. If fullname haven't 32th ASCII:
- B a o \0
- 0 1 2 3
- iLength = 3
- i = 3
- -> i = 0
- i = 0 -> k = 0 -> B
- i = 1 -> k = 1 -> a
- i = 2 -> k = 2 -> 0
- break -> k = 3 = '\0' -> \0
- // Create first name
- 1. If fullname include 32th ASCII:
- P h a n H u y n h G i a B a o \0
- 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
- i = 0
- -> i = 4
- for j = 0 -> 3
- j = 0 -> k = 0 -> P
- j = 1 -> k = 1 -> h
- j = 2 -> k = 2 -> a
- j = 3 -> k = 3 -> n
- break -> k = 4 -> \0
- 2. If fullname haven't 32th ASCII:
- P h a n \0
- 0 1 2 3 4
- i = 0
- -> i = 4
- for j = 0 -> 3
- j = 0 -> k = 0 -> P
- j = 1 -> k = 1 -> h
- j = 2 -> k = 2 -> a
- j = 3 -> k = 3 -> n
- break -> k = 4 -> \0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement