How do I select when to print specific characters in a character array [closed]


For my assignment I need to do the following

<ul><li>Ask the user to input their name and save it to a character array</li> <li>Then I need to output the number of characters input by said user</li> <li>Then I have to spell that user's name backwards</li> <li>followed by telling the user which part of the character array contained the space between their first and last name</li> <li>

The last thing that I need to do but can't quiet figure out (actually I'm completely lost) is output the user's last name first then first name last (ie John Doe = Doe John);

#include <stdio.h> #include <string.h> int main () { printf("Enter your name\n"); char name [25]={'\0'}; char * pch; fgets(name,sizeof(name),stdin); printf("You Entered: %s \n", name); printf("There are %u characters in your name including the space. \n", strlen(name)); char end; int i; end = strlen(name) -1; printf("Your name backwards is"); for (i = end; i >= 0; --i) { printf("%c", name [i]); } printf("\nLooking for the space in your name \n", name); pch=strchr(name, ' '); while (pch!=NULL) { printf("Space found at %d\n", pch-name+1); pch=strchr(pch+1, ' '); } } </li> </ul>

Anyone that knows how to do this Can you please explain what you did so I can learn from you?


Since you found the space, cut the string in half there by overwriting it with a '\0' character:

*pch = '\0';

Then print first the string following where the space was, and then the whole string:

printf("last name: %s\n", pch + 1); printf("first name: %s\n", name);

This will break somewhat if there are multiple spaces, but you get the idea.


You know the index where the space occured(pch here). Print pch+1 to end and then from 0 to pch if you are iterating the string by character.


You already found the location where the space in the name is in the second to last question. Just replace the space with a \0. Then printf the two strings:

*pch = '\0'; printf("%s %s\n",pch + 1,name);



The last thing that I need to do but can't quiet figure out (actually I'm completely lost) is output the users last name first then first name last (ie John Doe = Doe John);


It is common assignment/interview question of how to reverse an oder of strings in an array. An elegant albeit maybe not the most efficient algorithm would be:

step 1: reverse the array character by character step 2: reverse each string in-place

Check the answers <a href="https://stackoverflow.com/questions/1009160/reverse-the-ordering-of-words-in-a-string" rel="nofollow">here</a> for more info.


  • How to do custom-query based collection mappings in Hibernate?
  • Get back the assembly level code from exe files?
  • How to escape quotation marks in WinDbg's startup command line opening a dump
  • is the UPDATEIFCOPY flag ever True?
  • Multi-line JSON read using Apache PIG
  • fscanf not reading floats correctly
  • python regex split string while keeping delimiter with value
  • Call C++ class member function from C (Visual Studio)
  • Reaping zombie process - child
  • How to retrieve a specific string from a specific list from a file with JSON in Python
  • How to change default stop edit behavior in jtable
  • How to replace TouchesBegan with UIGestureRecognizer
  • Specify HTTPS for custom WCF Binding
  • Haskell: function composition with anonymous/lambda function
  • What's the name of this finding square root algorithm?
  • Making Google Visualization - Annotation Chart to work in GWT
  • Creating a Multidimensional, Associative Array in VBScript
  • cell spacing in div table
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Detecting null parameter in preprocessor macro
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Cast between interfaces whose interface signatures are same
  • How can I set a binding to a Combox in a UserControl?
  • Can I check if a recipient has an automatic reply before I send an email?
  • Highlight one bar in a series in highcharts?
  • QLineEdit password safety
  • How to extract text from Word files using C#?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • How to get Windows thread pool to call class member function?
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal