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



On Jul 25, 7:23 pm, "bpascal...@xxxxxxxxxxxxxx"
<bpascal...@xxxxxxxxxxxxxx> wrote:
Hi,

I'm posting quite a lot, i'm learning c without any solid programming
experience. So when my code doesn't work after looking why if i can't
get to know why, I ask here and i keep on coding as well.

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...

When that happens, it means the ends of your strings (the terminating
nul) is getting snipped. Step one: check loop conditions and array
lengths.

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.

I'd make a function to transpose one character (int tr(int)).
And then one to loop across the string calling that function
for each character. And I'd probably do it in place instead of
copying at to a new array (why would you need both?).

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] ;

        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) ;
                }

Your spacing makes this very difficult to read.
I don't understand why you need all that space.
I also don't understand why you're calling UppStrg
in this loop. You're incrementing the offset and
asking it to modify 40 elements starting at each position.
Every time through this loop after the first, you're
asking UppStrg to modify elements that are outside of
the bounds of the array.
UppStrg should be called once for each string.
If you need to verify that the string consists
only of lowercase characters, you can use some
kind of flag value that's set in the loop when
a bogus character is encountered and then check
after the loop whether it was set. Or you can
use some standard library functions. I recently
discovered this one:

if (strspn(s,"abcdefghijklmnopqrstuvwxyz") == strlen(s))
printf("char *s consists wholy of lowercase letters\n");


        } while ( (Txt1[i] <= 97 )   &&      (Txt1[i] >= 123) ) ;

        for ( i = 0     ;       i <= n       ;       i++ )
                putchar(Txt2[i]) ;
        printf("\n\n") ;

        return 0 ;

}

/* Function that should move a lowercase letter into uppercase */

void UppStrg(char *Low, char *Upp, int cnt)
{
        int i ;

        for ( i = 0     ;       i <= cnt     ;       cnt++ )
                *(Upp+i) = *(Low+i) - 'a' + 'A';

}



Dude, use the array notation for strings. It's prettier.
Upp[i] = Low[i] - 'a' + 'A';
I don't like the arithmetic here, either. It would make more
sense to me to add the difference rather than subtracting the
sum. But better that all these (IMHO) is

char *al="abcdefghijklmnopqrstuvwxyz";
char *AL="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
....
Upp[i] = AL[strchr(al, Low[i]) - al];

This way doesn't assume ASCII, but it assumes that you've already
made sure the string consists only of lowercase characters.
Otherwise strchr will return NULL and NULL - al is not a valid
index into the AL array.

--
lxt
.



Relevant Pages

  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Kevin Backmann ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • Re: Get the path and namefile in run time
    ... function Get_Path_Only return String; ... -- This returns the first N characters of the program name. ... end loop; ...
    (comp.lang.ada)

Loading