LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-11-2005, 07:40 PM   #1
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Case conversion in C


Hi folks,

Looking for a way to convert string to upper case in C. I have seen references to strupr( ) but it does not work for me.

I am looking for something that works something like this:
Code:
char *myString = "aBc123"
magicFunction( myString );
printf( "Upper case string: %s\n", myString );
//output is Upper case string: ABC123
As always, TIA for any help or links!
 
Old 09-11-2005, 08:11 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Code:
#include <ctype.h>
#include <string.h>

char *str="hello world";
int i;

for(i=0;i<strlen(str);i++)
    str[i]=toupper(str[i]);
Wrap it as a function or macro as needed.
 
Old 09-11-2005, 08:34 PM   #3
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi,

Thanks for the reply. That seems simple enough, and yet I am getting a segfault when I try to run it. It's in the line that makes the call to toupper, according to gdb. Here is what I have:
Code:
#include <ctype.h>
#include <string.h>

void to_upper( char *the_string );

int main( void ) {

        char *a = "aBc:123";
        to_upper( a );
        printf( "New string: %s\n", a );
        return( 0 );
}

void to_upper( char *the_string ) {

        int x;
        for( x = 0; x < strlen( the_string ); x++ )
                the_string[ x ] = toupper( the_string[ x ] ); // segfault here
        return;
}
Ideas?
 
Old 09-11-2005, 09:05 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by Matir
Code:
#include <ctype.h>
#include <string.h>

char *str="hello world";
int i;

for(i=0;i<strlen(str);i++)
    str[i]=toupper(str[i]);
Wrap it as a function or macro as needed.
There's a slight problem with the above code. You are trying to modify memory that is in the read-only data section, which will result in a segfault. (As Zaichik already found....)

The following modified code should work.

Code:
#include <ctype.h>
#include <string.h>

void to_upper( char *the_string );

int main( void ) {

        char a[] = "aBc:123";
        to_upper( a );
        printf( "New string: %s\n", a );
        return( 0 );
}

void to_upper( char *the_string ) {

        int x;
        for( x = 0; x < strlen( the_string ); x++ )
                the_string[ x ] = toupper( the_string[ x ] ); // segfault here
        return;
}
 
Old 09-11-2005, 09:14 PM   #5
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Yeah, actually, oddly enough, it seems that char *="VAL"; makes the variable a constant.

Changing it to be: "char a[]=" makes it work.
 
Old 09-11-2005, 09:14 PM   #6
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi,

How about this instead?
Code:
#include <ctype.h>
#include <string.h>

void to_upper( char *the_string );

int main( void ) {
        char *a;
        a = ( char * ) malloc( 12 );
        sprintf( a, "aBc:123" );
        to_upper( a );
        printf( "New string: %s\n", a );
        return( 0 );
}
void to_upper( char *the_string ) {

        int x;
        for( x = 0; x < strlen( the_string ); x++ )
                the_string[ x ] = toupper( the_string[ x ] );
        return;
}
That would be in the writable area, yes? And this compiles and runs, but the output is simply
New string:

 
Old 09-11-2005, 09:17 PM   #7
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Quote:
Originally posted by zaichik
Hi,

How about this instead?
Code:
#include <ctype.h>
#include <string.h>

void to_upper( char *the_string );

int main( void ) {
        char *a;
        a = ( char * ) malloc( 12 );
        sprintf( a, "aBc:123" );
        to_upper( a );
        printf( "New string: %s\n", a );
        return( 0 );
}
void to_upper( char *the_string ) {

        int x;
        for( x = 0; x < strlen( the_string ); x++ )
                the_string[ x ] = toupper( the_string[ x ] );
        return;
}
That would be in the writable area, yes? And this compiles and runs, but the output is simply
New string:

This code works fine for me.
 
Old 09-11-2005, 09:32 PM   #8
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hmm.

Thinking I was missing some includes, I added a couple, so now I have:

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

Still, the string is not printing out for me, neither the original nor the modified version.

?
 
Old 09-11-2005, 09:36 PM   #9
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Does compiling with -Wall reveal any more warnings? -pedantic?
 
Old 09-11-2005, 09:40 PM   #10
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Nope:
Code:
me@here# gcc -Wall -pedantic -o upper -g to_upper.c
me@here# ./upper
Old string:
New string:
me@here#
(I added a line in main() before the call to to_upper to print the string a after sprintf() ).

I am flummoxed.
 
Old 09-11-2005, 09:43 PM   #11
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
If you're familiar with gdb, have you tried stepping through it through gdb? What does sprintf return?
 
Old 09-11-2005, 10:05 PM   #12
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi David,


gdb showed me that sprintf() wasn't being called at all!
Code:
(gdb) break 1
Breakpoint 1 at 0x80483e4: file to_upper.c, line 1.

(gdb) run
Starting program: /home/justme/upper

Breakpoint 1, main () at to_upper.c:7
7       int main( void ) {
(gdb) step
10              a = ( char * ) malloc( 12 );
(gdb)
11              printf( "Old string: %s\n", a );
(gdb)
Old string:
12              to_upper( a );
(gdb) print a
$1 = 0x9dc5008 ""
(gdb) step
Somehow between copying the code for the earlier post and actually saving and compiling, the sprintf() call was lost entirely! Embarrassing!

Replaced and all is dandy now, thanks for your patience!
 
Old 09-11-2005, 10:18 PM   #13
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Lol. Fair enough. I'm glad to meet someone who knows his way around gdb.
 
Old 09-11-2005, 10:28 PM   #14
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Unfortunately, I can't say I know my way around it. I can compile with -g, and know how to set a break point, step, and print. I am sure it can do much more, like setting watches and maybe looking at the stack, but I don't know what. Once this project I am working on is done, I will spend some quality time with the man page for gdb.

Thanks again for your assistance!
 
Old 09-11-2005, 10:31 PM   #15
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Debugging core dumps is also helpful. And I'm no gdb power user, but the basics get 90% of the work done anyway.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C] Temperature conversion Mathiasdm Programming 18 08-24-2005 01:00 PM
Converting sLoPPy cASE to Pretty Case with tr lowpro2k3 Programming 4 04-13-2005 08:13 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
Lower case to upper case letter sudhasmyle Programming 1 12-03-2004 04:15 AM
case conversion roballen Linux - General 3 02-10-2004 02:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration