+1 (315) 557-6473 

Name and Number problem using C Assignment Solution

Number Separating First and Last Name Assignment

Once again we will be working with a list of names that includes everyone in our class, BUT this time each person’s name has embedded in the middle a secret number, so each line in the file looks like this:


Notice that there are no spaces separating the names or the numbers. Write a c assignment try to read in every line of the file “namesnum.txt” and to separate the secret number from each of the names, and then to print it at the end of the two names, with a space separating the names. So the output file, called secretnums.txt, will look something like this:

Jasson Aguilera 18791

And now the real fun. If we all work together we can open up the vault, and the key that opens the vault is actually the sum of everyone’s secret numbers.

The method:

Read a line into a single string

Using two counters walk through that string, copying one character at a time into the first name string until you reach a digit (For this you can use the isdigit function from ctype.h – it takes a single char argument (e.g. isdigit(fullname[j]);) and returns truthfulness.)

Put the ‘\0’ at the end of the string you have built.

Reset one of your counters and copy all the characters to another string in a loop, until you encounter a non-digit. Put the ‘\0’ at the end of the string you have built.

Reset one of the counters and copy all the characters to the end of the original string into still another string. Put the ‘\0’ at the end of the string you have built.

Use the function atoi, which takes a single string as an argument, and returns the integer represented by that string to extract the number from the second string you constructed. This function is in stdlib.h.

Add that number to a sum.

Output the strings and the number for each student to a file, with the number following the name. At the bottom output the secret number.


#include < stdio.h> #include < ctype.h> #include < string.h> #include < stdlib.h> /* Entry point of the program */ int main(int argc, char *argv[]) { FILE *file; char line[100]; char firstName[100]; char lastName[100]; char number[100]; int i, j; int sum; /* Open the input file */ file = fopen("namesnum.txt", "r"); if (!file) { /* Stop if file can't be opened */ printf("Error: Failed to open namesnum.txt"); return 0; } /* Read the file contents line by line */ sum = 0; while (fgets(line, 100, file)) { /* We extract the first name */ j = 0; for (i = 0; !isdigit(line[i]); i++) firstName[j++] = line[i]; firstName[j] = '\0'; /* Extract next the digits after the first name */ j = 0; for (; isdigit(line[i]); i++) number[j++] = line[i]; number[j] = '\0'; /* Extract the last number after the digits */ j = 0; for (; line[i] != '\n' && i < strlen(line); i++) lastName[j++] = line[i]; lastName[j] = '\0'; /* Print it out */ printf("%s %s %s\n", firstName, lastName, number); /* Total the sum */ sum += atoi(number); } /* Output the secret number */ printf("\n"); printf("Secret number: %d\n", sum); /* We're done, close the file */ fclose(file); return 0; }