Total members 11793 |It is currently Thu Aug 22, 2019 4:46 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





I'm trying to write a programme to convert lowercase to uppercase,


I wrote this myself(phew! wasn't easy)
cpp code
#include<stdio.h>
main()
{
char lett[80];
int convt,i=0;

convt='a'-'A';
printf("Enter the word in lowercase \n");
gets(lett);

for(i=0;(lett[i]>='a')&&(lett[i]<='z');i++)
{
if(lett[i]==' ')
printf("\t");

else lett[i]-=convt;

}

puts(lett);
}


But the problem is, after a space it doesn't convert to uppercase anymore, I've
tried some logic to fix it, (thats why the if statement is there) but it doesn't
work

I got this from the textbook I'm reading. it has the same problem too.


cpp code
#include<stdio.h>
main()
{
char str[80];
int i,delt='a'-'A';
printf("Enter a string less than 80 characters:\n");
gets(str);
i=0;
while(str[i])
{
if ((str[i]>='a')&&(str[i]<='z'))
str[i]-=delt;
++i;
}
printf("The entered string is (in uppercase):\n");
puts(str);
return 0;
}




By the way, the compiler (or software) I use is SILVERFROST(/PLATO);




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

The way you wrote the 'for' statement is
ingenious, but there's the problem, actually: the
'for' loop executes as long as the condition in
the second part between the parentheses holds.
You wrote: "loop as long as the i-th character in
the string is a lower-case letter." As soon as it
encounters a space (or any other character outside
that range, for that matter), it exits the loop.

What you really want is to loop through *all* the
characters in the string, since you don't know a
priori where the last lower-case character is. In
other words, you want to loop with 'i' varying
from the first index in the string to the last one
(which not necessarily will be 80).

_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time

Code:
#include<stdio.h>
#include<conio.h>

void main()
{
 
char arr[80];
 
int diff,i;
 
clrscr();
 
diff='a'-'A';
 
printf("\nenter a string= ");
 
gets(arr);
//scanf("%s",arr);

 
for(i=0;arr[i]!='\0';i++)
 {
  if(
arr[i]>='a' && arr[i]<='z')
    
arr[i]-= diff;
  else if(
arr[i]>='A' && arr[i]<='Z')
    {}
  else if(
arr[i]==' ')
    
printf("\t");
  else
    {}
  }
 
puts(arr);
 
//printf("\nthe upper case converted strins is =%s",arr);
 
getch();




Author:
Post new topic Reply to topic  [ 3 posts ] 

  Related Posts  to : convert lowercase to uppercase
 add string to another, Lowercase ,Uppercase     -  
 lowercase table content     -  
 force letters to be lowercase text-transform     -  
 How to Convert WMV to AVI on Mac     -  
 convert word, Excel to PDF     -  
 convert timestamp using javascript     -  
 convert string into binary     -  
 convert to binary number     -  
 convert XMLGregorianCalendar to GregorianCalendar     -  



Topic Tags

C++ Strings






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com