code not working properly

it try to make my own code for touperr() function
but their is some problem which i am unable to figure out
so help me out.
code id as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include<conio.h>
using namespace std;
char* mytoupper(char* );
int main()
{
    char *s;
    s=(char *)malloc(20*sizeof(char));
    puts("Enter the string :");
    gets(s);
    puts("The string is :");
    puts(s);
    s=mytoupper(s);
    puts("The string in upper case is :");
    puts(s);
    getch();
    return 0;
}
char* mytoupper(char* s)
{
      int i;
      while(*s!='\0')
      {
                     if(*s>='a' || *s<='z')
                     {
                               i=*s-97;
                               *s='A'+i;
                     }
                     cout<<*s;
                     i=0;
                     s++;
      }
      cout<<"\n";
      return s;
}                          

my query is that why the last puts(s) statement in main function doesn't giving any correct output...
Instead of while(*s!='\0') i think you need to use some sort of strcmp() function to compare the characters. Same goes for the other comparison tests.
No, everything is fine, expet for the pus and gets functions, probably becouse I use c++11, and conio.h isn't c++ etx etx blah blah blah. But you may want to put the
s=mytoupper(s);
After the
puts("The string in upper case is :");
becouse there was some weird result on my comp with that, that I can't fugure out.
puts() and gets() are defined in cstdio. However
man wrote:
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security.


1
2
3
4
5
6
s=mytoupper(s); //the call

  //the function
      while(*s!='\0')
      //...
      return s;
You are making 's' to point to the end of the string.


Also
1
2
3
//    char *s;
//    s=(char *)malloc(20*sizeof(char));
char s[20];
Adding to everything said above.

This condition is incorrect and will actually be true for any character in the string: if(*s>='a' || *s<='z') - it should be if(*s>='a' && *s<='z')
Topic archived. No new replies allowed.