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

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

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++;
     }  
}
Member Avatar for iamthwee

Did you read my link first.

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

Member Avatar for iamthwee

Please do. :)

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

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++;
     }
     
}
#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 [B]fgets( filename, sizeof filename / sizeof ( char ), stdin );[/B] */

while (filename[i] != "\0"); /* "\0" is a string and is not the same that [B]'\0'[/B], 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 */

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:

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

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

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

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

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]);
           [B]++i; /* increment the counter */[/B]
     }
     
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.