There are many problems with your code:
#include<stdio.h>
Use proper spacing: #include <stdio.h>
.
While most spacing is not required, consistent use of spaces and indentation greatly enhances code readability and reduces the number of bugs. If you use sloppy style, you probably also use buggy algorithms and careless constructs. Bugs will have more places to hide. C is a very sharp tool, it is unforgiving with the careless.
#include<conio.h>
This header is an obsolete, Microsoft specific thing. It is not required here.
#include <ctype.h>
ctype.h
defines the right functions for your assignment, but you don't even use them!
int main()
Define main
as int main(void)
or int main(int argc, char *argv[])
{
char name[100];
int loop;
printf("Enter any Sting: ");
So this whole thing was just a sting? Interesting lapsus!
gets(name);
Definitely never ever use gets
! If the user types more than 99 characters at the prompt, gets
will cause a buffer overflow as it cannot determine the size of name
. This buffer overflow will most likely cause the program to crash, but careful exploitation of such a flaw can allow an attacker to take control of the computer. This is a very good and simple example of a security flaw. scanf
is more complicated, inefficient and difficult to use safely in this context, as in most. Just use fgets
and handle the trailing '\n'
appropriately.
for (loop=0; name[loop] !=0; loop++)
The common idiom for this loop is to use an index variable called i
. Naming it loop
is confusing and verbose. The comparison to 0
could be written more simply as name[loop]
is your teacher condones it, or name[loop] == '\0'
to make it clear that you are comparing characters.
{
if(name[loop]>='A' && name[loop]<='Z')
You should use isupper
to test for upper-case. Comparing explicitly to character values 'A'
and 'Z'
is non portable and error prone as you don't seem to get your comparisons right.
name[loop]=name[loop]+32;
You are assuming ASCII or similar encoding. This dirty trick is non portable, confusing and error prone. Just use the tolower
function.
else if(name[loop]>'a' && name[loop]<='z')
Same remark as above. Your code is buggy. Proper use of spacing and less verbose index name should make the bug more obvious:
else if (name[i] > 'a' && name[i] <= 'z')
Use islower
for both portability and reliability.
name[loop]=name[loop]-32;
See above. toupper
was designed for this purpose.
}
printf("\nConvert case of character : %s",name);
Fix the broken English. Also add a '\n'
at the end of the format string. Some systems will not even output anything if you don't.
return 0;
}
Proofing your homework online is only useful if you learn from it. I hope you did!