code improvement

hi guys!
can i further improve the code?
thanks!

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
/*
warinigs: 0
errors:   0
*/
void main()
{
clrscr();
int i=0,j=0;
char ch[255],k;
cout<<" ===-===-=== "<<endl;
cout<<"|Text Status|"<<endl;
cout<<" ===-===-=== "<<endl<<endl;
cout<<"Type something: ";
gets(ch);
cout<<"the text is: "<<ch;
cout<<endl<<endl;

//prinr each char
{
for(j=0;j<=strlen(ch);j++){
cout<<ch[i]<<endl;
i++;
}
}

//string len
{
j=i=0;
cout<<endl<<"length of string [incl.spaces]: "<<strlen(ch)<<endl;
}


//capitilaze
{
cout<<"capitol: ";
for(j=0;j<=strlen(ch);j++){
k=toupper(ch[i]);
i++;
cout<<k;
}
}

//text reverse
{
cout<<endl<<"reverse: ";
j=0,i=strlen(ch);
for(j=0;j<=strlen(ch);j++){
cout<<ch[i];
i--;
}
}

getch(); //wait for user input
}
//end of program 
still got no reply?
can i further improve the code?

Probably the biggest improvement will come from finding, downloading, and using a modern C++ compiler. Any compiler that allows #include<iostream.h> is much too outdated to be using. And once you start using that modern compiler make sure you enable, at minimum, compiling in C++11 mode, if not C++14 mode to take advantage of the modern C++ features available in the current C++ standards.

The next necessary improvement will come from finding and consistently using some kind of indentation style. https://en.wikipedia.org/wiki/Indent_style

Next, I recommend you stop using the outdated conio.h header file and anything related to it, unless absolutely necessary (it's not really necessary in this program).

Lastly, never, Never, NEVER use gets(), this C function is so dangerous that it has actually been removed from the latest C and C++ standards.

So to sum it up you need to start learning Modern C++ not pre-standard C++ from 20 years ago and avoid using all those C'isms such as C-strings, strlen(), getch(), etc. and start using real C++ features such as std::string, std::vector, std::array, etc..

First suggestion, get a modern compiler. It looks like you're using Turbo C which is antiquated and not standards compliant.

Line 1-6: Modern C++ headers do not use .h

Line 3: Get rid of conio.h. Not standards compliant.

Line 15: Use std:;string, rather than C-strings.

Line 20: Don't use C style I/O.

Line 26,35,42,52,53: strlen is C. Use string::size().

Last edited on
thanks. can you suggest a modern compiler ?
From #include<conio.h> you appear to be on a Windows platform.

Go for Visual Studio 2017 Community Edition. https://www.visualstudio.com/vs/

In the installer, select 'Desktop Development with C++'

In the summary pane on the right, select both:
'VC++ 2017 v141 toolset' (the microsoft compiler and tool-chain ) and
'Clang/C2' (the clang++ frontend)

That is all that you need. For a light download and installation, uncheck everything else.
(You can add things - eg. the standard library as a set of modules or CMake - later if you feel the need for it.)
Topic archived. No new replies allowed.