Thread: Is this the best way to write this?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    32

    Is this the best way to write this?

    Hey guys,
    This program does exactly what it is intended to do, I was just wondering if it is the best way to accomplish this task. The program searches 'sentence' for str1, str2, or str3 (as picked by the user), and displays whether or not it is found in the original sentence. I am learning C from a book and doing all of the challenges, like I said, this program works fine, just wondering if it is the BEST way doing this.

    Thanks!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    main(){
        char *sentence="When the going gets tough, the tough stay put!";
        char *str1="Going";
        char *str2="tou";
        char *str3="ay put";
        int response=0;
        
        printf("%s\n\n", sentence);
        
        printf("1\t is \"%s\" in the sentence?\n", str1);
        printf("2\t is \"%s\" in the sentence?\n", str2);
        printf("3\t is \"%s\" in the sentence?\n", str3);
        printf("4\t Quit\n\nPlease enter your choice...");
        scanf("%d", &response);
        
        switch(response){
            case 1:
                if (strstr(sentence,str1)!=NULL){  
                    printf("Yes \"%s\" is found in %s\n", str1, sentence);
                    getch();
                }
                else
                    printf("Nope, the G is not capitalized in \"%s\"", sentence);
                    getch();
                return;
            case 2:
                if (strstr(sentence, str2)!=NULL){
                    printf("Yes, \"%s\" is found in \"%s\"\n", str2, sentence);
                    getch();
                }
                else
                    printf("Nope!");
                    getch();
                return;
            case 3:
                if (strstr(sentence, str3)!=NULL){
                    printf("Yes, \"%s\" is found in \"%s\"\n", str3, sentence);
                    getch();
                }
                else
                    printf("Nope!");
                return;
            case 4:
                return;
        }
    }
    Last edited by Mark Labarbara; 11-24-2011 at 12:05 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    First of all, main must be declared as
    int main(void)
    You also need to reutrn a value, typically zero.
    Most of your returns have the same effect as a break so I would just change them to breaks.
    Some of your cases call getch() twice, once inside the if and once after the else. That's right the second one is not inside the else, but you're misleading yourself into thinking that it is because of the indentation. After changing the returns to break and putting a single return at the end, why not put the getch() at the end too.
    You don't need a case such as case 4 if it does nothing different from the default case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Thanks for the critique

    I am curious about a few things though (please forgive me, I am quite new to this)...
    Why does main have to be declared int main(void)? Will it effect more complex programs?
    The reason I had two getch was because I thought they were nested in the if AND else.
    I thought that if I put getch after the else, it would only be processed if the else statement were
    true, not the if. Thanks for clarifying that for me. How would you get it to process only if the else is
    true though? Would you use curly brackets for the else?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    holy crap what a coincidence. Its damn near the same thing I'm working on.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    So would this be the best way to write the program?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main(void){
        char *sentence="When the going gets tough, the tough stay put!";
        char *str1="Going";
        char *str2="tou";
        char *str3="ay put";
        int response=0;
        
        printf("%s\n\n", sentence);
        
        printf("1\t is \"%s\" in the sentence?\n", str1);
        printf("2\t is \"%s\" in the sentence?\n", str2);
        printf("3\t is \"%s\" in the sentence?\n", str3);
        printf("4\t Quit\n\nPlease enter your choice...");
        scanf("%d", &response);
        
        switch(response){
            case 1:
                if (strstr(sentence,str1)!=NULL)
                    printf("Yes \"%s\" is found in %s\n", str1, sentence);
                else
                    printf("Nope, the G is not capitalized in \"%s\"", sentence);
                getch();
                break;
            case 2:
                if (strstr(sentence, str2)!=NULL)
                    printf("Yes, \"%s\" is found in \"%s\"\n", str2, sentence);
                else
                    printf("Nope!");
                getch();
                break;
            case 3:
                if (strstr(sentence, str3)!=NULL)
                    printf("Yes, \"%s\" is found in \"%s\"\n", str3, sentence);
                else
                    printf("Nope!");
                getch();
                break;
        }
        return 0;
    }
    Last edited by Mark Labarbara; 11-24-2011 at 12:39 PM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Quote Originally Posted by A34Chris View Post
    holy crap what a coincidence. Its damn near the same thing I'm working on.
    Lol, nice!

    What book are you using? I am using "C Programming for the absolute beginner, Second Edition by Michael Vine"

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mark Labarbara View Post
    I am curious about a few things though (please forgive me, I am quite new to this)...
    Why does main have to be declared int main(void)? Will it effect more complex programs?
    Actually it affects the operating system. An operating system such as windows expects a return value from a program, usually as a means of reporting errors. This value can be used in parent programs, batch or script files, etc. as a working value. Your program may work well enough with ... main() and no return value, but it is incorrectly interfaced with the OS.

    The reason I had two getch was because I thought they were nested in the if AND else.
    You should think strategically when programming. Get your code to work, but then do an extra pass aimed at simplifying it without losing functionality with less code. That would be the underpinning of iMalc's suggestion in this case. On some of my larger projects I've removed several k of code and still had a perfectly functional solution...
    I thought that if I put getch after the else, it would only be processed if the else statement were
    true, not the if. Thanks for clarifying that for me. How would you get it to process only if the else is
    true though? Would you use curly brackets for the else?[/QUOTE]

    Yep, braces would do that for you... but when you have it in both if and else clauses, it runs every time anyway, so you can eliminate the duplication.

    Another simplification...
    Code:
                if (strstr(sentence,str1)!=NULL){  
                    printf("Yes \"%s\" is found in %s\n", str1, sentence);
                    getch();
                }
                else {
                    printf("Nope, the G is not capitalized in \"%s\"", sentence);
                    getch();
               }
    
    // is the same as...
                if (strstr(sentence,str1)!=NULL)  
                    printf("Yes \"%s\" is found in %s\n", str1, sentence);
                else
                    printf("Nope, the G is not capitalized in \"%s\"", sentence);
    
                getch();

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There's usually no "best way" to write something. It depends on the criteria. For instance, maybe you're looking for something more scalable so you can easily add more search options, like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    int main(void){
      char *sentence="When the going gets tough, the tough stay put!";
      char *searches[] = { "Going", "tou", "ay put" };
      int num_searches = 3;
      int response;
      int i;
    
      printf("%s\n\n", sentence);
         
      for(i = 0;i < num_searches;++i)
        printf("%d\t is \"%s\" in the sentence?\n", i + 1, searches[i]);
      printf("%d\t Quit\n\nPlease enter your choice...", i + 1);
      scanf("%d", &response);
      getchar();
    
      if(response > 0 && response <= num_searches)
      {
        if(strstr(sentence, searches[response - 1])!=NULL)
          printf("Yes \"%s\" is found in \"%s\"\n", searches[response - 1], sentence);
        else
          puts("Nope!");
      }
    
      getchar();
      return 0;
    }
    Last edited by itsme86; 11-24-2011 at 12:48 PM.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void){
        char *sentence="When the going gets tough, the tough stay put!";
        char str[3][7]= {"Going", "tou", "ay put"};
        int response=0;
        
        printf("%s\n\n", sentence);
        
        printf("1\t is \"%s\" in the sentence?\n", str[0]);
        printf("2\t is \"%s\" in the sentence?\n", str[1]);
        printf("3\t is \"%s\" in the sentence?\n", str[2]);
        printf("4\t Quit\n\nPlease enter your choice...");
        scanf("%d", &response);
               
        if (response < 0  || response > 3)
          return 0;
    
        if ( strstr(sentence,str[response -1]))
          printf("Yep, it's there.");
        else
          printf("Sorry, not there.");
    
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    Awesome!

    Thanks everyone for your tips and suggestions, I really appreciate it

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by CommonTater View Post
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void){
        char *sentence="When the going gets tough, the tough stay put!";
        char str[3][7]= {"Going", "tou", "ay put"};
        int response=0;
        
        printf("%s\n\n", sentence);
        
        printf("1\t is \"%s\" in the sentence?\n", str[0]);
        printf("2\t is \"%s\" in the sentence?\n", str[1]);
        printf("3\t is \"%s\" in the sentence?\n", str[2]);
        printf("4\t Quit\n\nPlease enter your choice...");
        scanf("%d", &response);
               
        if (response < 0  || response > 3)
          return 0;
    
        if ( strstr(sentence,str[response -1]))
          printf("Yep, it's there.");
        else
          printf("Sorry, not there.");
    
        return 0;
    }
    Why wouldn't you use a loop like I did for displaying the options? If you have an array, might as well make effective use of it.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itsme86 View Post
    Why wouldn't you use a loop like I did for displaying the options? If you have an array, might as well make effective use of it.
    You already did that. I was presenting a different option... just like you did.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what to write
    By lukasjob in forum C Programming
    Replies: 8
    Last Post: 04-02-2011, 03:54 AM
  2. What's the best way to write this?
    By claphahat in forum C Programming
    Replies: 4
    Last Post: 07-29-2010, 04:53 AM
  3. any other way you'd write this?
    By withoutn in forum C Programming
    Replies: 14
    Last Post: 07-16-2007, 11:26 AM
  4. problem with fout.write: cannot write 0x0A (10 dec)
    By yvesdefrenne in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2005, 12:53 PM
  5. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM