I have searched google and the forums here and I couldn't find what i need, if there is a thread somewhere just forward me to it.

I am attempting to create a program that will prompt a user for a string. The program will not recognize this string to be finished until it is ended with a period.

I think i can figure out how to do the program, thanks to some useful links on google. My issue is that don't know how to keep prompting the user until the sentence is ended with a period. The period will not be printed out in the output string.

input - My name is Dewey<ret>(program still expects input)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input - My name is Dewey.<ret>(program runs)
output - Dewey is name My

So what's the problem?

Stopping when you see a "." ?
Reading a line?
Extracting words from a line?

Or just printing them in reverse?

Show us where you can get to yourself.

the problem for now is that i don't know how to make sure the user uses a "." to end the sentence. I'm pretty sure i can find out how to do the rest thanks to google.

My issue is that don't know how to keep prompting the user until the sentence is ended with a period.

This is all you have to do to prompt the user till he enters a string with a period:

char msg[50];
    int length = 0;
    do
    {
        printf("Enter a string and end it with a period\n");
        fgets(msg,50,stdin);
        length = strlen(msg);  //We do this since fgets appends a 
        msg[length-1] = '\0';   // '\n' to 'msg' and it would make 
        length = strlen(msg);  //'length' one more than what it really should be.
    }while(msg[length-1]!='.');

Thanks devnar

For some reason this is what happens when run:
Enter a sentence to be reversed:
My name is Dewey

D(no¤ªê¿

the random symbols will change for different input while the first letter of output is always the first letter of the last word of input... I'm really confused by this, if anybody knows how to fix this let me know if not ill post code tomorrow.

It usually happens if the string isn't NULL terminated. It would be easier to figure out your problem if you posted your code.

Also, in my previous code, you'll have to flush the input before reading in the input again. Gotta do it at the beginning of the do-while loop (Hope i'm doing it right :) ).

char msg[50];
    int length = 0,ch;
    do
    {
        while( (ch=fgetc(stdin)) != EOF && ch != '\n'  )
                 ;
        printf("Enter a string and end it with a period\n");
        fgets(msg,50,stdin);
        length = strlen(msg);  //We do this since fgets appends a 
        msg[length-1] = '\0';   // '\n' to 'msg' and it would make 
        length = strlen(msg);  //'length' one more than what it really should be.
    }while(msg[length-1]!='.');

it works without that extra while loop with it i need to put a period then on the new line put another period.

heres my code

#include <stdio.h>
#include <string.h>

int reverseStr(char *str);
void reverseTxt(char *c1, char *c2);

int main(int argc, char *argv[]){
  char sentence[60], reversed[60];
  int length = 0;

  printf("\nInsert sentence to be reversed: \n");

  do{

    fgets( sentence, 60, stdin );
    length = strlen( sentence );
    sentence[length - 1] = '\0';
    length = strlen( sentence );

  }while( sentence[length - 1] != '.');
  *reversed = reverseStr( sentence );
  printf("\n %s \n", reversed);
  return(0);
}


int reverseStr(char *str){
  char *c1, *c2;

  if(strlen(str) > 0){
    reverseTxt(str, str + strlen(str) - 1);

    c1 = str, c2 = str + 1;
    do{
      for( ;*c2 != ' ' && *c2; c2++ );

      reverseTxt(c1, c2 - 1);
      if( !*c2 ) break;
      c1 = ++c2;
    }while(*c2);
  }
  return( *str);
}

void reverseTxt(char *c1, char *c2){
  char ch;

  while( c1 < c2 ){
    ch = *c1;
    *c1++ = *c2;
    *c2-- = ch;
  }
}

it works without that extra while loop with it i need to put a period then on the new line put another period.

Hmm. I guess I misjudged the working of fgetc(stdin). Will look that up.

That's quite a complex piece of code you got there. Since I'm still a beginner it took me quite a while to understand it's logic. I'm just surprised that you could come up with that logic and not be able to figure out that period-at-the-end problem (unless of course you copied). :P Anyway, i'll give you the benefit of the doubt. Here's your code debugged:

#include <stdio.h>
#include<stdlib.h>
#include<string.h>

#include <stdio.h>
#include <string.h>

void reverseStr(char *str);
void reverseTxt(char *c1, char *c2);

int main(int argc, char *argv[]){
  char sentence[60]; //reversed[60] not needed
  int length = 0;

  printf("\nInsert sentence to be reversed: \n");

  do{

    fgets( sentence, 60, stdin );
    length = strlen( sentence );
    sentence[length - 1] = '\0';
    length = strlen( sentence );

  }while( sentence[length - 1] != '.');
  sentence[length-1] = '\0';//since you don't want period at the end
  reverseStr( sentence );
  printf("\n%s \n", sentence);// Why would you wanna do all the operation 
                             //on 'sentence' and then print 'reversed'?
  getchar();
  return(0);
}


void reverseStr(char *str){
  char *c1, *c2;

  if(strlen(str) > 0){
    reverseTxt(str, str + strlen(str) - 1);

    c1 = str, c2 = str + 1;
    do{
      for( ;*c2 != ' ' && *c2; c2++ );
        reverseTxt(c1, c2 - 1);
      if( !*c2 )
        break;
      c1 = ++c2;
    }while(*c2);
  }
  return;
}

void reverseTxt(char *c1, char *c2){
  char ch;

  while( c1 < c2 ){
    ch = *c1;
    *c1++ = *c2;
    *c2-- = ch;
  }
}

i half copied the code, i studied the code so i understood it, copied it, changed it to fit my needs, got errors and posted it here. thanks a lot it works great now.

This question has already been answered. Start a new discussion instead.