Re: Help a beginner - simple lowercase to uppercase and so on function
- From: "bartc" <bartc@xxxxxxxxxx>
- Date: Sun, 26 Jul 2009 00:55:57 GMT
<bpascal123@xxxxxxxxxxxxxx> wrote in message news:754ef01c-2aeb-4adb-ba9e-c49d60e0728f@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
So below is a simple lowercase to uppercase code from a to z that
doesn't fully work. I use gcc on linux and i've tried on gcc on
Windows and the output is quite the same (however nothing is return in
djgpp windows).
In gcc linux, It takes the input but it returns it after some delay
and it adds some characters i have never seen at this stage of
learning and nowwhere since i use a computer...
I don't know if it's the right way to do this, i wish pointers
wouldn't be involved for this. I can't think of a way to do this with
functions to split actions. Once this part below works, i'd like to
add some more string functions on the model of the function (UppStrg)
below :
#include <stdio.h>
#include <string.h>
void UppStrg(char *Low, char *Upp, int cnt) ;
int main(void)
{
int n = 40 ;
char Txt1[n] ;
char Txt2[n] ;
You're using variable arrays here. Not causing your problems but take a bit more care to use.
int i ;
printf("\n\nThis program reads and converts : 1- a lower case string
into upper case : \n") ;
do
{
printf("\nEnter a lowercase string : \n") ;
scanf("%s", Txt1) ;
n = strlen(Txt1) ;
for ( i = 0 ; i <= n ; i++ )
{
if ( (Txt1[i] <= 'a' ) || (Txt1[i] >= 'z') ) /* IF checks if there
are any characters other than lowercase letters */
printf("\nThere are no lowercase letters in this string !\n\n") ;
else
UppStrg(&Txt1[i], &Txt2[i], n) ;
}
} while ( (Txt1[i] <= 97 ) && (Txt1[i] >= 123) ) ;
These 2 nested loops are a mess. Once you've detected a character is not lower case, you want to break out of the inner loop.
The Uppstrg() functions converts a single character, or a whole string? If a whole string, it should be outside the inner loop, with Tx1 and Txt2 arguments. If a single character, you shouldn't use "&", and you need to rewrite UppStrg! This is mainly why the program is doing strange things.
The conditional to the outer do-while loop is meaningless. 'i' is undefined at this point. (I'm not familiar with how scanf forces a new input line, so I'd would read the input a little differently myself, perhaps using fgets() or getchar())
Your for-loop is also looking at the 0-terminator of the string, which is not necessary; make i count from 0 to <n.
void UppStrg(char *Low, char *Upp, int cnt)
{
int i ;
for ( i = 0 ; i <= cnt ; cnt++ )
*(Upp+i) = *(Low+i) - 'a' + 'A';
If you're going to assume Ascii code, then just subtract 32 from the character code...
Usually case conversion code will work with any character, and ignore anything not a..z or A..Z, depending which way it's going. (There are anyway standard C functions for case-converting characters or strings.)
--
bartc
.
- References:
- Help a beginner - simple lowercase to uppercase and so on function
- From: bpascal123@xxxxxxxxxxxxxx
- Help a beginner - simple lowercase to uppercase and so on function
- Prev by Date: Re: help a beginner : code not working
- Next by Date: Re: Help a beginner - simple lowercase to uppercase and so on function
- Previous by thread: Help a beginner - simple lowercase to uppercase and so on function
- Next by thread: Re: Help a beginner - simple lowercase to uppercase and so on function
- Index(es):
Relevant Pages
|