Sign in to follow this  
gaiger

I have a problem....

Recommended Posts

#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()
{
cout<<"Enter your name:";
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
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]).

Admiral

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by gaiger
Nope 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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this