12 Replies - 4676 Views - Last Post: 18 February 2013 - 03:10 AM Rate Topic: -----

#1 mikelino  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 03-November 12

gets() and puts() problem

Posted 13 February 2013 - 08:14 AM

 #include<stdio.h>
  #include<string.h>
  int main(){
  char str[20];
  int i;
  printf("Enter any string->");
  gets(str);
  printf("The string is->%s",str);
  for(i=0;i<=strlen(str);i++){
      if(str[i]>64&&str[i]<91)
          str[i]=str[i]+32;
          
      else if(str[i]>95&&str[i]<122)
          str[i]=str[i]-32;
          
   }
   puts(str);
   getch();
   return 0; 
 }



can anyone tell me what's wrong with the code??
the question demand to use gets() and puts() to shown the answer.It will print back the original string and then only the converted string.My tutor say "getch()"is under <conio.h> file header but along programming practice i did not put in the header file it work also right?
Or there is other way to do it by using <ctype.h> toupper/tolower to do such a question?Kindly seek guide. :shuriken:

Is This A Good Question/Topic? 0
  • +

Replies To: gets() and puts() problem

#2 michael072  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 100
  • Joined: 08-February 11

Re: gets() and puts() problem

Posted 13 February 2013 - 09:00 AM

I'm not sure I understand your question.

The first thing I will say is NEVER use gets. With gets there is no checking done to see if the input string will fit into the buffer. This is very dangerous because all sorts of security flaws can come from a malicious person being able to overflow a buffer. You should use fgets. With fgets you can specify to only read in 20 characters or however big your buffer happens to be.

One problem is you didn't include <conio.h> int your code but you did mention it.

To accomplish the same thing as this code using toupper/tolower you would iterate through the string like you're doing now but instead of doing
str[i]=str[i]+32;
you would call toUpper(str[i]) to convert the lower case to upper case and toLower(str[i])
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: gets() and puts() problem

Posted 13 February 2013 - 09:56 AM

On a tangent:
<rant>
Funny how the Internet community has gotten behind "don't use gets() because it can overrun the buffer", but there isn't an equal amount of force behind "don't use strcpy() because it can overrun the buffer".
</rant>
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,688
  • Joined: 25-December 09

Re: gets() and puts() problem

Posted 13 February 2013 - 10:19 AM

That's because with gets() you are relying on the user to know not to enter a string that is too long. With strcpy() you the programmer are the one responsible to know the size of your strings. You the programmer can take steps to insure that you the programmer don't misuse strcpy(). But you the programmer have absolutely no control of that errant user.

Jim
Was This Post Helpful? 3
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: gets() and puts() problem

Posted 13 February 2013 - 12:40 PM

Ah! The piece that I was missing! Thanks jimblumberg.
Was This Post Helpful? 0
  • +
  • -

#6 mikelino  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 03-November 12

Re: gets() and puts() problem

Posted 14 February 2013 - 02:22 PM

If you run the code, you will see the result annoying.Example:you enter"hi",the result produce should be "HI" but it will produce "hiHI'.
The question insist must use gets() and puts(),i have limited choice. :withstupid:
I did not include <conio.h>.Is that means that some of the function will not running well or what??As you can see that "getch();" is still running well right?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,688
  • Joined: 25-December 09

Re: gets() and puts() problem

Posted 14 February 2013 - 02:55 PM

Quote

I did not include <conio.h>.Is that means that some of the function will not running well or what??As you can see that "getch();" is still running well right?

Yes it means that your program should at minimum issue a warning about the missing include file. Most modern compilers will produce an error message when you don't include the proper header files for the functions you use.

Quote

The question insist must use gets() and puts(),i have limited choice.

If your instructor insists on using gets(), you need to find a new instructor. This function has been removed from the current C standard because it is so dangerous. There are alternative functions that can be used that are considered acceptable, such as fgets().

Quote

If you run the code, you will see the result annoying.Example:you enter"hi",the result produce should be "HI" but it will produce "hiHI'.

What do you expect your program to produce. You have told it to print "hi" with the code on line 8 of your post and then you print "HI" on line 17.

I also recommend you replace the integer constants you are using in your program:
str[i]>95&&str[i]<122
with their character equivalents. If you do you will see that your "numbers" are actually incorrect. See this link ASCII. Using the decimal value of 96 is the '`' character which is not a Alpha character! Decimal 122 is the character 'z' and since you used the operator< you exclude the character 'z' from your comparison meaning a 'z' will not be converted to upper case. Your really should use the toupper() or tolower() functions instead of your calculations.

Jim

This post has been edited by jimblumberg: 14 February 2013 - 02:55 PM

Was This Post Helpful? 1
  • +
  • -

#8 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: gets() and puts() problem

Posted 14 February 2013 - 06:50 PM

<conio.h> isn't required or even mentioned in standard C. As this header isn't mentioned, even if it exists, it's not required to expose what you think it might. getch() might be non-existant, or a function that returns pointer to communist_nation. The <stdio.h> header is mentioned in standard C. If it exists, it's required by standard C to expose the function getchar(), which is required to return an int. The int will be negative when getchar() fails, or a positive unsigned char value when it succeeds. You're using getch() at the end, presumably to keep the program open until a key is struck. getchar() can be used for this purpose. Why not use the standard function?

On a related topic, presumably you're developing a console application. How many console applications do you know that pause until a key is struck at the end? Does ls or dir do this? What about cd? Why not? Perhaps you could justify the pause as debug functionality. In that case, why not use the same strategy as assert? For example:

#include <stdio.h>
#include <assert.h>
#ifndef NDEBUG
#    define return_pause puts("Press any key to continue."); \
                         getchar(); \
                         return
#else
#    define return_pause return
#endif

int main(void) {
    puts("Hello, world!");
    return_pause 0;
}


The pause can be suppressed by #defining NDEBUG, which toolchains tend to do automatically when you compile for "release".

This post has been edited by undefined behaviour: 14 February 2013 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#9 mikelino  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 03-November 12

Re: gets() and puts() problem

Posted 17 February 2013 - 07:07 AM

Thanks boss.I am kinda blur while thinking of the code.Anyway
 
                   

   if(str[i]>64&&str[i]<91)
           str[i]=toupper(str[i]);      
      else if(str[i]>96&&str[i]<123)
           str[i]=tolower(str[i]);




i don't think it make any diffrence.LOL :withstupid:
is that others way to read the string and convert where upper case become lower case and vice versa? :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,688
  • Joined: 25-December 09

Re: gets() and puts() problem

Posted 17 February 2013 - 07:26 AM

What is the purpose of the following code?
if(str[i]>64&&str[i]<91)
        str[i]=toupper(str[i]);      
   else if(str[i]>96&&str[i]<123)
        str[i]=tolower(str[i]);

What exactly is the purpose of the magic numbers, 64, 91, 96, and 123? This would probably be clearer:
if(str[i] >= 'A' && str[i] <= 'Z')
        str[i]=toupper(str[i]);      
   else 
        str[i]=tolower(str[i]);

Also in this situation you don't need the "else if" a simple else would be sufficient.

Are you trying to reverse the case of your character? Or are you just trying to make all of your characters either upper or lower case?

Instead of all this I would do something like the following to reverse the case of each letter:

if(islower(str[i])
   toupper(str[i]);
else
   tolower(str[i]);




That is if I am allowed to use the functions contained in ctype.h

Jim

This post has been edited by jimblumberg: 17 February 2013 - 07:27 AM

Was This Post Helpful? 2
  • +
  • -

#11 ashokkumar01cbe  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 04-December 12

Re: gets() and puts() problem

Posted 18 February 2013 - 12:17 AM

hey remove the
 printf("The string is->%s",str);
then it works fine. in 6th line it just prints the input which you have typed. then after computation in the loop it prints the output.
Was This Post Helpful? 0
  • +
  • -

#12 mikelino  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 03-November 12

Re: gets() and puts() problem

Posted 18 February 2013 - 02:55 AM

PRO!EXPERT!
I respect you! :^:
Would you mind to be my new instructor??
^_^
Add you friends man ^^

You are late.i know the mistake already.
I am same with you, zero reputation.
Damn shame in this forum...........
not giving you reputation :bananaman:
Was This Post Helpful? 0
  • +
  • -

#13 mikelino  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 03-November 12

Re: gets() and puts() problem

Posted 18 February 2013 - 03:10 AM

JIM ^_^
you are the reason for me to join this forum.
Thanks very much for your guidance.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1