Thread: Is there another way to write this program?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Question Is there another way to write this program?

    Hi. Our assignment was to write a program that accepts lines of text which the user types, and display them in the politically correct non-sexist form by changing all occurrences of the strings "man" to "person" and "Man" to "Person."

    For example:
    Enter a string: Amanda Peterman

    Apersonda Peterperson



    PHP Code:
    #include <stdio.h>
    #include <string.h>

    char *replace(char userstr[]);
    void main(){
      
    char userstr[100];


      
    printf("Please enter a line *if you wish to exit, press Enter* : ");
      
    /*Now goes to function replace*/
      
    replace(userstr);


    }


    char *replace(char userstr[]){
      
    int i;

      
    /*Unless the user doesn't enter anything, it will go through these steps*/
      
    while(gets(userstr) && strlen(userstr) != 0){
        
    0;

         while(
    != strlen(userstr)){
         
    /* while loop - while the length of the user's string doesn't equal i(0), it will go through these steps*/

          /* it will go through these steps if "Man is in a string */
          
    if(*(userstr i) == 'M' && *(userstr 1) == 'a' && *(userstr +
            
    2) == 'n'){
                    
    printf("Person");
                    
    3;
          }
          
    /* else if "Man is in a string it will go through these steps*/
          
    else if(*(userstr i) == 'm' && *(userstr 1) == 'a' && *(userstr +
            
    2) == 'n'){
                    
    printf("person");
                    
    3;
          }
          
    /* else if "man" and "Man" is not found print the current character*/
          
    else{
                    
    printf("%c", *(userstr i));
                    
    i++;
          }
        }

        
    printf("\n\nPlease enter a line *if you wish to exit, press Enter*: ");
      }
         return 
    0;

    Now my program works the way it should but, my professor wants it arranged so that in the main program, it reads the string the user inputs, passes it to function replace, replace function converts it, return it back to main program, & prints the new string there. And he wants me to do all of this without using any of the string functions!!!! He wants us to use our own versions of the string functions. I don't see how this could possibly work, but if anyone can see how you can arrange it that way could you please show me how?

    Thank You.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    First: the only string function there is strlen which is easily implementable. If you cant' use the strings library, you'll implement each of the function you'll use.
    Consider a string like "HELLO". In memory it'll be like
    Code:
           H  E  L  L  O  0
           |  |  |  |  |  |  
    index: 0  1  2  3  4  5
    Now think how you can, given any string, to find the null char that ends the string? You''l have to use a loop. You'll also need a strcpy. Using the representation above, instead of just counting chars, assign them to a new string till the 0 is found.

    Second:
    Code:
    while(gets(userstr) && strlen(userstr) != 0)
    CHANGE TO
    while(fgets(stdin,100,userstr) && strlen(userstr) <=1)
    to prevent buffer overflow. Imagine that the user inserts more than 100 characters... Yes no place in string. And strlen(...)<=1 because fgets stores newlines ('\n').

    Third, the function to replace may receive a string and send another back with the needed changes:
    Code:
    void change_words(char* dest, const char* src){
        int index=0, index;
        loop:
           -check if chars at positions index,index+1,index+2 are 'm''a''n'
           -if so copy to dest "person", and move dest pointer 5 positions ahead
           -if not check if chars at positions index,index+1,index+2 are 'M''a''n'
                   -if so copy to dest "Person", and move dest pointer 5 positions ahead
                   -if not, no match was found, so place in dest, the char from src in position index,
                              and move the dest pointer to next position
           -continue loop if index is not in the end of src, increment index
    }
    If you don't know what I mean bit change the position of a pointer, consider again the little representation of the string above.
    Code:
    char *str="HELLO";//a)
    str++;//b)
    str+=4;//c)
    In a) dereferencing str ( *str) results in char 'H'
    In b) dereferencing str ( *str) results in char 'E', because the ponter was moved to the next char
    In c) dereferencing str ( *str) results in 0, the terminating character, because the pointer is moved 4 chars ahead, to the end of the string.
    All of these chars could have been read with calls to str[0], str[1] and str[5].

    If you're stiil reading this, go to this link please
    Last edited by xErath; 11-23-2004 at 10:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  3. how could i write this program: cat < apa | wc | wc > bepa
    By strugglingman in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 04:40 PM
  4. Replies: 1
    Last Post: 10-13-2004, 12:15 PM
  5. Challenge to write a program
    By Twisted.alice in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-15-2003, 12:00 PM