inputs a string from the user and coverts all the capital letters to small letters and all the small letters to capital letters.
Example:
If the user enters string “HeLLo wOrLd”, the program will display it as “hElllO WoRlD”

i have written following code.. please tell where im wrong..as it is only converting small letters to capital letters..and not converting capital letters to small...

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{
clrscr();
int count=0;

char a[1000];
int s,i;

cin.getline(a,1000);




for (s=0;a[s]!='\0';s++)
{count++;}




for (i=0;i<count;i++)
{

if ((a[i]>=65) && (a[i]<=90))
{a[i]=a[i]+32;}


if ((a[i]>=97) && (a[i]<=122))
{a[i]=a[i]-32;}
}



puts(a);



getch();
}
WaltP commented: The title is used for a description of the problem, the post is where the question is asked, which includes infomation about your problem. -3

I would use the functions toupper or tolower to make your code portable.

int ans = toupper(val);
if (ans == val)
{
/*is upper case*/
}
else
{
/*is lower case*/
}

try google for isupper toupper etc.

never mind beat to the punch.

Your two if statements are executed every time in sequence, so if you have an upper case letter, it'll be converted to lower case, then back to upper case. If you have a lower case letter, it'll jump over the first if statement and still be converted to upper case.

You need an else if rather than a second independent if:

if ((a[i]>=65) && (a[i]<=90))
    a[i]=a[i]+32;
else if ((a[i]>=97) && (a[i]<=122))
    a[i]=a[i]-32;

Your code is horrible, by the way. I'm just not in the mood to correct everything right now...

commented: indeed +17

we are not allowed to use ctype i.e isupper toupper etc. cuz our teachr dnt allow us to use it...

thanks Narue it solved my problm

Narue@ code is horrible lol. i knw u ll say to use <iostream> instead of iostream.h and conio.h is non standard header file..etc lol i knw everything but im using TURBO C++ version 3.0 it require these things

if ctype is not allowed.......
Are you allowed to use other headers at all??

>i knw everything but im using TURBO C++ version 3.0 it require these things
The problems you mentioned were only the tip of the iceberg and could easily be written off after learning you're forced to use Turbo C++. I'd go into more detail but you deleted your post's contents like a freaking moron. Thanks for destroying thread continuity.

commented: Homework kiddo's ...*sigh* +16

Narue@ code is horrible lol. i knw u ll say to use <iostream> instead of iostream.h and conio.h is non standard header file..etc lol i knw everything but im using TURBO C++ version 3.0 it require these things

conio.h is not and never has been required.

If you know everything, then write your programs as properly as possible and use proper English. "i knw u ll" is not proper English.

>> but you deleted your post's contents like a freaking moron. Thanks for destroying thread continuity.

I've put the text back in the first post. If the OP was worried about his prof finding this code online, (s)he should've thought about that before clicking the "post new thread" button :icon_wink:

If you know everything, then write your programs as properly as possible and use proper English. "i knw u ll" is not proper English.

Waltp

This is not an english classes or forum. i dont like that.! Take things easy will you?

>I've put the text back in the first post.
Excellent, now the thread is useful again. Though I don't think I'll be helping the OP anytime soon, after that wonderful display of community service.

>This is not an english classes or forum.
It's a matter of practicality. If you're too lazy to put any effort into asking questions, we assume you'll be equally lazy in interpreting and implementing the answers. Such wastes of time are avoided by the more clueful people around here, which means you'll likely be ignored by the most qualified helpers.

Waltp

This is not an english classes or forum. i dont like that.! Take things easy will you?

No it's not an English class. As for an English forum, whether you like it or not, from the Member Rules:

Members who break a rule will usually be warned, [this is an infraction warning] followed by a points-based infraction for subsequent offenses. When a member is given an infraction, they accrue points which remain on their record for a time period....

Keep it Clear
* Do post in full-sentence English
* Do not write in all uppercase or use "leet", "txt" or "chatroom" speak

After a 1.5 years and 430 posts, you should be aware of these rules by now.

And rather than getting an infraction, which is completely justified, he has been warned outside that system, giving an extra chance to keep it clear. More than fair, eh?

commented: Good point +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.