# I have a problem....

## Recommended Posts

gaiger    100
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
int x,y;
char name[20],lo[20],up[20],rev[20];
main()
{
cin>>name;
y=strlen(name);
for(x=0;x<y;x++)
{
if(tolower(name[x]))
lo[x]=tolower(name[x]);

}
for(x=0;x<y;x++)
{
if(toupper(name[x]))
up[x]=toupper(name[x]);
}
for(x=0;x<20;x++)
rev[x]=name[y-x-1];
cout<<"\nLower case: "<<lo;
cout<<"\nUpper case is: "<<up;
cout<<"\nReverse is: "<<rev;
getch();
}

I need to change this code that will not using strlen if i remove the strlen the reverse name will not appear.. can you help me with it??

##### Share on other sites
ToohrVyk    1596
Why do you need to remove strlen?

##### Share on other sites
Jan-Lieuwe    206
Homework?

If your teacher told you not to use strlen, then maybe you should change the loop so that it stops when you encounter the terminator of the string ('\0').

There're many issues with your code, though.

##### Share on other sites
gaiger    100
Im just curious with it... I want some unique ideas and to do with that i need to remove the strlen... Because someone in my classmate ideas is same in my code... Not good reason but the only way to resolve this thing is to solve this with help or without help...

##### Share on other sites
If you need to find out the length of the string, then you need to find out the length of the string.
Any alternatives we propose would either require a fairly major rewrite, or an equivalent function (lstrlen? [wink]).

##### Share on other sites
gaiger    100
But my problem is not to use any string function unless the only way to find it out is to reverse the character of my code is using the string function then i will use it but not the strlen code...

##### Share on other sites
Zahlman    1682
0) We don't do homework here.

1) There are huge problems with what you are being taught. I strongly recommend you drop this course because it will not teach you anything properly.

1a) For example, there is really no such thing as iostream.h any more.
1b) Noone in the real world does string manipulation at this level without a really good reason.
1c) Reading into a buffer of constant size limits the size of strings (to the buffer size) for absolutely no good reason (because the new C++ standard library provides tools that get around that limit).

2) Do you know how strlen() works? If so, then the obvious thing would just be to implement it yourself, right?

##### Share on other sites
Crypter    748
Impliment your own strlen() (Just loop until you find the null terminator)

Is this homework?

##### Share on other sites
gaiger    100
Nope this is not a homework actually my friend told me this problem... My friend is a genius in C++ and he told me if i create a program that will appear the uppercase, lowercase, and reverse without using strupr, strlwr, strrev and strlen thats all...

##### Share on other sites
Koobazaur    1264
Educate yourself on the C ways of char arrays and the integer representations of ASCII characters and you can do it without ever including <string>

##### Share on other sites
Crypter    748
Quote:
 Nope this is not a homework actually my friend told me this problem... My friend is a genius in C++ and he told me if i create a program that will appear the uppercase, lowercase, and reverse without using strupr, strlwr, strrev and strlen thats all...

An easy way of doing that is simply using toupper() and tolower()
(#include <ctype>)

To reverse the string, just loop through it backwords.

Heres a possible strlen (Not tested nor compilied. It should help
you out though):
int strlen (char* str) {   if (!str)      return 0;   int ret=0;   while (str[ret++]!='/0');   return ret;}

It isnt that hard.

btw, this would be coinsidered 'c+' not C++. In C++, anyone should
tell you to use std::string, which is the standard.

Why would your friend tell you to do this anyway?

##### Share on other sites
Zahlman    1682
Quote:
 Original post by gaigerNope this is not a homework actually my friend told me this problem... My friend is a genius in C++

No, he isn't. He may think he is, and he may have you convinced he is, but geniuses don't waste their time on things like this.

Or perhaps he *is* a genius but is also a sadist :)