well in this program while i was debugging my cursor fly over two while statements can any one describe my why
-----> (where problem is)
#include<conio.h>
#include<stdio.h>
void main ()
{
char arr[20];
int i=0,j=0,k=0;
int a;
printf("enter alphabets\n");
while((arr[i]=getche())!='\r');
{
i=i+1;
}
;
printf("\n\nstring in lower case\n");
-----> while (arr[j]!='\r')
{
//upper case to lower case
if (arr[j]>=65&&arr[j]<=90)
{
arr[j]=arr[j]+32;
}
printf("%c",arr[j]);
j++;
}
printf("\n\nstring in upper case\n");
------> while (arr[k]!='\r')
{
//lower case to upper case
if (arr[k]>=97&&arr[j]<=122)
{
arr[k]=arr[k]-32;
}
printf("%c",arr[k]);
k++;
}
while((arr[i]=getche())!='\r') ; // <--- semicolon not needed
{
i=i+1;
}
; // another not needed, but harmless here.
The first loop has an extraneous semicolon on line 1. That means counter i is never incremented, all the input is stored in arr[0]
Other than the semicolon, it's fine - though Cire is correct that you risk exceeding the array boundary.
But personally I'd prefer to replace the stored '\r' with the null character in the array. That means it can then be treated as a standard null-terminated string, for example using puts(arr).
My idea, just for consideration as an alternative, in the final loop.
#include<conio.h>
#include<stdio.h>
void main ()
{
char arr[200];
int i=0;
int j=0;
int k=0;
int a;
printf("enter alphabets\n");
while ( (arr[i]=getche()) != '\r')
{
i=i+1;
}
arr[i] = '\0'; // add null terminator
printf("\n\nstring in lower case\n");
while (arr[j] != '\0')
{
//upper case to lower case
if (arr[j]>=65&&arr[j]<=90)
{
arr[j]=arr[j]+32;
}
printf("%c",arr[j]);
j++;
}
printf("\n\nstring in upper case\n");
while (arr[k])
{
//lower case to upper case
if (arr[k]>=97&&arr[j]<=122)
{
arr[k]=arr[k]-32;
}
k++;
}
puts(arr);
getch();
}
Notice line 35 is simpler this way. while (arr[k])