944,110 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4826
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 30th, 2007
0

Converting lowercase to capital

Expand Post »
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.
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #define CHARSIZE 101
  6.  
  7. int main()
  8. {
  9. char filename[CHARSIZE];
  10.  
  11. printf("Enter the filename that you want to open. /n");
  12. gets(filename);
  13.  
  14. toupper(filename);
  15.  
  16. puts(filename);
  17.  
  18. return 0;
  19. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theteamdrunk is offline Offline
17 posts
since Oct 2007
Oct 30th, 2007
0

Re: Converting lowercase to capital

>>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);
Last edited by Ancient Dragon; Oct 30th, 2007 at 2:42 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Oct 30th, 2007
0

Re: Converting lowercase to capital

You might also find the following tutorial useful...
http://www.daniweb.com/tutorials/tutorial45806.html
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 30th, 2007
0

Re: Converting lowercase to capital

have revised, It still doesnt work exactly though
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #define CHARSIZE 101
  6.  
  7. void convert (char strng[]);
  8.  
  9. int main()
  10. {
  11. char filename[CHARSIZE];
  12.  
  13. printf("Enter the filename that you want to open. /n");
  14. gets(filename);
  15.  
  16. convert(filename);
  17.  
  18. puts(filename);
  19.  
  20. return 0;
  21. }
  22. void convert()
  23. {
  24. int i =0;
  25.  
  26. while (filename[i] != "\0");
  27. {
  28. filename[i] = toupper(filename[i]);
  29. i++;
  30. }
  31. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theteamdrunk is offline Offline
17 posts
since Oct 2007
Oct 30th, 2007
0

Re: Converting lowercase to capital

Did you read my link first.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 30th, 2007
0

Re: Converting lowercase to capital

sorry, I think I was posting when your reply was posted, I am checking it out now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theteamdrunk is offline Offline
17 posts
since Oct 2007
Oct 30th, 2007
0

Re: Converting lowercase to capital

Please do.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 30th, 2007
0

Re: Converting lowercase to capital

>>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().
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Oct 30th, 2007
0

Re: Converting lowercase to capital

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:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #define CHARSIZE 101
  6.  
  7. void convert (char filename[]);
  8.  
  9. int main()
  10. {
  11.  
  12. char filename[CHARSIZE];
  13.  
  14. printf("Enter the filename that you want to open. /n");
  15. gets(filename);
  16.  
  17. convert(filename);
  18.  
  19. puts(filename);
  20.  
  21. return 0;
  22. }
  23. void convert(char* filename)
  24. {
  25. int i =0;
  26.  
  27. while (filename[i] != "\0");
  28. {
  29. filename[i] = tolower(filename[i]);
  30. i++;
  31. }
  32.  
  33. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
theteamdrunk is offline Offline
17 posts
since Oct 2007
Oct 30th, 2007
0

Re: Converting lowercase to capital

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

while (filename[i] != "\0"); /* "\0" is a string and is not the same that '\0', 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 */
Last edited by Aia; Oct 30th, 2007 at 8:15 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Problem in C program
Next Thread in C Forum Timeline: prime





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC