954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting lowercase to capital

I am writing code that accepts a string of a filename, then capitalize the letters, not changing any numbers or special characters. I think I am on the right path, but can some one give a little push to the right direction.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define CHARSIZE 101

int main()
{
    char filename[CHARSIZE];
    
    printf("Enter the filename that you want to open. /n");
    gets(filename);
    
    toupper(filename);
    
    puts(filename);
    
    return 0;  
}
theteamdrunk
Newbie Poster
17 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

>>toupper(filename);
Nope. toupper() only accepts a singlle character. There are no standard functions that converts the entire string, so you have to write that part youself. Put the below in a loop and you will have it.
filename[0] = toupper(filename[0]);

>>gets(filename);
Never ever use gets(). use fgets() instead like this: fgets(filename,CHARSIZE,stdin);

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

You might also find the following tutorial useful...
http://www.daniweb.com/tutorials/tutorial45806.html

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

have revised, It still doesnt work exactly though

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define CHARSIZE 101

void convert (char strng[]);

int main()
{
    char filename[CHARSIZE];
    
    printf("Enter the filename that you want to open. /n");
    gets(filename);
    
    convert(filename);
    
    puts(filename);

    return 0;   
}
void convert()
{
     int i =0;
     
     while (filename[i] != "\0");
     {
           filename[i] = toupper(filename[i]);
           i++;
     }  
}
theteamdrunk
Newbie Poster
17 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Did you read my link first.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

sorry, I think I was posting when your reply was posted, I am checking it out now.

theteamdrunk
Newbie Poster
17 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Please do. :)

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

>>have revised, It still doesnt work exactly though
You need to pass your convert() function the filename because its declared in main() and not visible to convert().

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I read the link, but dont know how it can help me. I have made a few changes, but cant get the desired result. Here is updated code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define CHARSIZE 101

void convert (char filename[]);

int main()
{

    char filename[CHARSIZE];
    
    printf("Enter the filename that you want to open. /n");
    gets(filename);
    
    convert(filename);
    
    puts(filename);

    return 0;   
}
void convert(char* filename)
{
     int i =0;
     
     while (filename[i] != "\0");
     {
           filename[i] = tolower(filename[i]);
           i++;
     }
     
}
theteamdrunk
Newbie Poster
17 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
#include <string.h> /* no need for this include */
#include <stdlib.h> /* no need for this include */

void convert (char filename[]); /* function is defined as char *filename not char filename[], even if it works like that */

printf("Enter the filename that you want to open. /n") /* you need \n which is not the same that /n */

 gets(filename); /* forget about gets, use <strong>fgets( filename, sizeof filename / sizeof ( char ), stdin );</strong> */

while (filename[i] != "\0"); /* "\0" is a string and is not the same that <strong>'\0'</strong>, which is what you want; futhermore that ending (;) is screwing the program there, remove it */


 filename[i] = tolower(filename[i]); /*  I though you wanted to convert to uppercase, use toupper() for that */
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Is your task to input a file name, and convert the filename itself to upper case? Or are you supposed to
1) open the file
2) read the file
3) convert the contents of the file to upper case?

And if you don't take the hint on gets() , we may stop helping you :icon_wink:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

>>void convert (char filename[]); /* function is defined as char *filename not char filename[], even if it works like that */

they are both the same so it doesn't matter. But they should be consistent not because the compiler will care but because good programming style.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

okay, so I have gotten it revised down, but the only problem I get when I compile is it is telling me that in the convert() function the i is undeclared. What should I declard it as.
Here is the code.

#include <stdio.h>
#include <ctype.h>
#define CHARSIZE 101

void convert (char filename[]);

int main()
{

    char filename[CHARSIZE];
    
    printf("Enter the filename that you want to open. /n");
    fgets(filename, sizeof filename / sizeof (char), stdin);
    
    convert(filename);
    
    puts(filename);
    fgets(filename, sizeof filename / sizeof (char), stdin);
    return 0;   
}
void convert(char* filename)
{
     while(filename[i] != '\0') 
     {
           filename[i] = toupper(filename[i]);
     } 
}
theteamdrunk
Newbie Poster
17 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

>What should I declard it as.
int, perhaps? And initialize it to 0? i is a counter that starts at the beginning of the string and looks for the null character. Are you really trying to say that you don't understand how an array index works?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thats what i tried, it ran the program but let me enter a filename then stopped;

void convert(char* filename)
{
    int i = 0;
     
     while(filename[i] != '\0') 
     {
           filename[i] = toupper(filename[i]);
     }
     
}
theteamdrunk
Newbie Poster
17 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
thats what i tried, it ran the program but let me enter a filename then stopped;
void convert(char* filename)
{
    int i = 0;
     
     while(filename[i] != '\0') 
     {
           filename[i] = toupper(filename[i]);
           <strong>++i; /* increment the counter */</strong>
     }
     
}
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You