Question: Q2. Write code to split an input string (variable name ) into two output strings (variables first and last ). Assume that the user provides
Q2. Write code to split an input string (variable name) into two output strings (variables first and last). Assume that the user provides input containing only the characters a through z and A through Z. Assume there are exactly two capital letters in the input, one at the beginning of the first name, and one at the beginning of the last name. For example, given the input JoeSmith, your code should split it into Joe and Smith. Please do not use inbuilt C string manipulation library functions.
So far I have this:
/*Assignment 2 Problem 2*/
#include
int main() { /*Set up variables and arrays for input and division of name*/ char name[40], first[20], last[20]; int l,i,j,k;
/* Ask user for the name*/ printf("Enter your name with no spaces and capitalize first and last initial. (Ex:JohnDoe) "); scanf("%s", &name); /*Find the length of the string and split it at the capital letter*/ for (l = 0; name[l] != '\0'; l++) { for (i = 1; i < l; i++) { if(name[i] >= 40 && name[i] <= 80) { j = i - 1; break; } } }
for (k = 0; k <= j; k++) first[k] = name[k]; for(j = 0; name[i] != '\0'; j++) { last[j] = name[i]; i++; }
last[j] = '\0'; /*Print the name with a space between first and last*/ printf("%s %s ", first, last); return 0; }
I am not sure what is causing errors, I'm not great working with arrays. It splits up the name but when certain longer names are entered, some strange characters show up.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
