Re: Help a beginner - simple lowercase to uppercase and so on function




<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

.



Relevant Pages

  • Re: Standard function to convert " " to (etc.)?
    ... decodes all these escapes back into a string. ... corresponding character. ... int convert_escape{ ... #define ESCAPE 1 ...
    (comp.lang.c)
  • RE: DTS How to parse a varcharfield
    ... Nick Barclay created a UDF function that parses a varchar field. ... It returns the position of the character AFTER the nth(i.e. ... you know the positions you could update the varchar string by substituting ... returns int ...
    (microsoft.public.sqlserver.dts)
  • Re: RC4 algorithm problem
    ... > int lengthOfData; ... default character encoding, at least when any of the characters are out ... Is using an ASCII string directly as the RC4 key a recommended practice? ...
    (sci.crypt)
  • Re: Strange strcmp() problem
    ... I would *guess* that you failed to notice an opening '(' character on your test string. ... How will your program react to a number greater than int being put in? ... Many experts consider that the best way to capture input is to accept the input characters into a string, and then pick the string apart in more detail - that is, to separate the task of capture from the task of validation. ... All of these are palindromes, but none will pass your test. ...
    (comp.lang.c)
  • Re: atoi
    ... int char2int ... functions for character classification. ... so you want to give the caller a bit more flexibility. ... But fgetsdoesn't *always* leave a '\n' in the string. ...
    (comp.lang.c)