pasteC
What's going wrongunknown
plain_text
4 years ago
1.5 kB
7
Indexable
#include <stdio.h> #include <string.h> int main(){ // declare two strings large enough for most names char name[200], official[200]; // declare an iterator variable and a variable for the number of parts of the name int i, parts; // read the name from the screen fgets(name, sizeof name, stdin); // set the number of parts to 0 parts = 0; // iterate through the string, until we reach the end for(i = 0;i < 200 && name[i] != '\0';i++){ // if the current char is a space, put a '\0' for the "end" of the string and increment the number of parts if(name[i] == ' '){ name[i] = '\0'; parts++; } } // set the current char to an end of line char name[i] = EOF; // iterate to the start of the last name while(i > 0 && name[i] != '\0') i--; // add the lastName to the official string and add ", " strcpy(official, name + i + 1); strcat(official, "\b, "); // reset i to 0 i = 0; // while there are more than 0 parts to be printed, decrement parts while(--parts > 0){ // iterate to the start of the next part of the name while(name[i++] != '\0'); // add the part of the name and a space strcat(official, name + i); strcat(official, " "); } // add the first char of the first name name[1] = '\0'; strcat(official, name); // show the result on the screen printf("%s.\n", official); }
Editor is loading...