Thread: can someone help me out? i have no idea what to do

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    8

    Post can someone help me out? i have no idea what to do

    can someone help me out? i have no idea what to do-help-pls-png

    I'm new in C and can't solve this Q if someone can help me solve this thing i will be grateful.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,532
    How hard did you try?

    Surely you can get as far as
    Code:
    int main ( ) {
      printf("Enter a sentence: ");
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    8
    Quote Originally Posted by Salem View Post
    How hard did you try?

    Surely you can get as far as
    Code:
    int main ( ) {
      printf("Enter a sentence: ");
    }
    I can take the sentence from user.

    But i can't switch the words.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,532
    So post what you have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    8
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    
    void Replace(char * );
    int main() {
            char second[80];
            char first[80];
        printf("Enter a sentence : ");
        gets(first);
    
    
        second[80] = Replace(first);
    
    
        system("PAUSE");
        return 0;
    }
    
    
    void Replace(char * snt) {
        
    }
    I couldn't do more.

  6. #6
    Registered User
    Join Date
    Mar 2017
    Posts
    8
    Quote Originally Posted by cmuchhard View Post
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    
    void Replace(char * );
    int main() {
            char second[80];
            char first[80];
        printf("Enter a sentence : ");
        gets(first);
    
    
        second[80] = Replace(first);
    
    
        system("PAUSE");
        return 0;
    }
    
    
    void Replace(char * snt) {
        
    }
    I couldn't do more.
    do i need to use pointer ?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,532
    So where you do prompt for the replacement word?

    A useful function to call may bestrstr
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    You will probably need to use several built in functions using the standard c library, and as the instructions states, you will have to create a single user defined function. The best c library to start out with is the string library: C Library <string.h>

    Anyways, if you were to do this in C++, I think it would be much easier, due to the C++ standard template library. But with c, you'll have to go over the built in library, most notably, probably the string library, and see how much of the library's functions will help you complete this program. The rest will be up to you.

    If you try to build all your own functions, when they already exist (pre made), you are making this much harder for yourself. Although your teacher may want you to build your own functions, the instructions only say 1 user defined function.
    Last edited by Terrance; 03-19-2017 at 08:17 AM.

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    Code:
        printf("Enter a sentence : ");
        gets(first);
    Please DO NOT use gets()! Use fgets() instead.

    What if the user typed in a line longer than 79 characters? It would overwrite any data that in memory after the "first" array.

    Code:
        system("PAUSE");
    Better to use a simple getchar(). "system("PAUSE")" is for Windows only and is not portable.

  10. #10
    Registered User
    Join Date
    Mar 2017
    Posts
    8
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    
    void Replace(char*, char*,char*  );
    int main() {
    		char second[80];
    	    char first[80];
    		char aim[20];
    		char New[20];
    
    
    	printf("Enter a sentence : ");
    	gets(first);
    
    
    	second[80] = Replace(first);
    
    
    	printf("Enter the word you want to replace : ");
    	gets(target);
    
    
    	printf("Enter the new word : ");
    	gets(New);
    
    
    	system("PAUSE");
    	return 0;
    }
    
    
    void Replace(char*first, char*aim, char*New ) {
    	
    }
    Can i handle this job with only one function?

  11. #11
    Registered User
    Join Date
    Mar 2017
    Posts
    8
    Quote Originally Posted by cmuchhard View Post
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    
    void Replace(char*, char*,char*  );
    int main() {
            char second[80];
            char first[80];
            char aim[20];
            char New[20];
    
    
        printf("Enter a sentence : ");
        gets(first);
    
    
        second[80] = Replace(first);
    
    
        printf("Enter the word you want to replace : ");
        gets(target);
    
    
        printf("Enter the new word : ");
        gets(New);
    
    
        system("PAUSE");
        return 0;
    }
    
    
    void Replace(char*first, char*aim, char*New ) {
        
    }
    Can i handle this job with only one function?
    const char or just char ?

  12. #12
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by cmuchhard View Post
    Can i handle this job with only one function?
    Probably not. I looked up similar questions on Google, and it required several built in string functions to complete. As Salem mentioned, a good function to start with is strstr in string.h.

    Anyways, think about how you would do this in Microsoft Excel. The underlying logic is the same, but you will have to write out the code.
    Last edited by Terrance; 03-19-2017 at 08:46 AM.

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    Think about what you are trying to do, and work it out on paper first. What Standard Library functions do you need to use? What loops and control statements will you need.

    1) Enter the string to search.
    2) Enter a word to search for.
    3) Search for the word.
    4) If not found, ask the user for another word. Go back to Step 2 & 3.
    5) Once you have found the word to replace, ask the user for the replacement word.
    6) Replace the word, and display the corrected string to the user.

  14. #14
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    678
    @cmuchhard:

    I have to ask. How are you learning C? From a qualified instructor, a book, YouTube videos, or some other source?

    Thank you.

  15. #15
    Registered User
    Join Date
    Mar 2017
    Posts
    8
    Quote Originally Posted by rstanley View Post
    @cmuchhard:

    I have to ask. How are you learning C? From a qualified instructor, a book, YouTube videos, or some other source?

    Thank you.
    I'm learning C from youtube.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My idea.
    By Zach Sisk in forum C Programming
    Replies: 39
    Last Post: 09-17-2012, 02:43 PM
  2. No idea on how to do this...
    By Sembhi in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2007, 08:26 PM
  3. Idea
    By RPW in forum Contests Board
    Replies: 0
    Last Post: 01-07-2003, 07:50 AM
  4. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 12:26 PM

Tags for this Thread