Changing this to user prompt help.

This is a discussion on Changing this to user prompt help. within the C Programming forums, part of the General Programming Boards category; All I need help with is to change the last part to a user prompt instead of just already having ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Changing this to user prompt help.

    All I need help with is to change the last part to a user prompt instead of just already having the value entered within the program. The part I need to change is bold. This program is supposed to reverse the word order. i.e apples and oranges = oranges and apples.

    Can someone write it please?

    I was thinking something like:

    printf("Enter phrase> ");
    scanf("this is where i'm lost to scan multiple words");
    printf("here too");


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    
    void reverse (char *s, int len)
    {
      int i, j;
    
      for (i = 0, j = len - 1; i < j; i++, j--) {
        char save = s[i];
        s[i] = s[j];
        s[j] = save;
      }
    }
    
    void revword (char *s)
    {
      while (*s != '\0') {
        char *e = s;
        /* Find the end of a word */
        while (*e != '\0' && !isspace (*e))
          e++;
        reverse (s, e - s);
        /* Find the next word */
        s = e;
        while (*s != '\0' && isspace (*s))
          s++;
      }
    }
    
    
    int main (void)
    {
      char s[] = ("apples and oranges");
      reverse (s, strlen (s));
      revword (s);
      puts (s);
    
      getch();
      return 0;
    }
    Last edited by Infuriate; 12-03-2005 at 05:21 PM.

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    is it always going to be: something AND something? As in, apples and oranges, or grapes and plums, or is there a possibility there will be more than 3 words?

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    more than 3 words is possible, so:

    how now brown cow

    =

    cow brown now how
    Last edited by Infuriate; 12-03-2005 at 05:29 PM.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    here is a sample code which solves your problem. and i belive this code still be improved. but any way a quick code

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void reverse(char *);
    void revword(char *);
    
    char rev[20];
    
    int main()
    {
     	char string[]={"how now brown cow"};
     	reversed=malloc(20);
     	
     	reverse(string);
     	
     	printf("%s",rev);
     	
     	getchar();
    }
    
    void reverse(char *string)
    {
     	 char temp[20];
    	 int max=strlen(string),i=0;
    	 char *p;
    	  max-=1; 
    	  
     	 while( max>=0 )
     	 {
    	  	temp[i++]=string[max];
    	  	if(string[max]==' ' || max ==0)
    	  	{
    		    temp[i++]='\0';
    		    revword(temp);
    		    strcat(rev,temp);
    		    strcat(rev," ");
    		    i=0;
    		}
    		max--;
        }
    }
    
    void revword(char *s)
    {
      int i, j=strlen(s);
      char save;
      j-=1;
      for (i = 0; i < j; i++, j--) {
        save = s[i];
        s[i] = s[j];
        s[j] = save;
      }
    }
    
    /* my output
     cow  brown  now how
    */
    if you go through the code the logic is very simple

    ssharish2005

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    char *mystrrev(char *s, size_t size)
    {
       char *start = s, *t = s + size;
       for ( --t ; s < t; ++s, --t )
       {
          char temp = *s;
          *s = *t;
          *t = temp;
       }
       return start;
    }
    
    char *foo(char *s)
    {
       char *start = s, *t = s;
       while ( *s )
       {
          while ( *t && !isspace(*t) )
          {
             ++t;
          }
          mystrrev(s, t - s);
          while ( *t && isspace(*t) )
          {
             ++t;
          }
          s = t;
       }
       return start;
    }
    
    int main()
    {
       char text[] = "how now brown cow";
       puts(text);
       puts(mystrrev(text, strlen(text)));
       puts(foo(text));
       return 0;
    }
    
    /* my output
    how now brown cow
    woc nworb won woh
    cow brown now how
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    char s[] = ("apples and oranges");
    ->
    Code:
    char s[] = "apples and oranges";
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 05:01 AM
  2. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 06:37 AM
  3. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. changing the prompt???
    By JimJamJovi in forum C Programming
    Replies: 13
    Last Post: 01-09-2002, 08:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21