3 Replies - 690 Views - Last Post: 19 April 2013 - 08:07 PM Rate Topic: -----

#1 thesyntaxdr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 19-April 13

converting string array to uppercase

Posted 19 April 2013 - 06:37 PM

Hi everyone,

I am new to coding so i really appreciate the help. Here is the problem.

Have a program prompt the user for a filename to open. Change every alphabetic character in the file to a capital letter. Numbers and special characters should not be changed. Print the output to the screen.

Here is my code so far but i am only returning the last line of text capitalized from the file. I am trying to get the program to display all of the three lines of text from the file capitalized. The program displays the file correctly before i try and convert everything toupper();


Thanks in advance for the help
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

int main()
{
	char line[81], filename[21];
	double price;
	int i;
	FILE *inFile;
	
	printf("\nPlease enter the name of a file: ");
	gets(filename);
	inFile = fopen(filename, "r");
	if (inFile == NULL)
	{
		printf("\nThe file %s was not successfully opened.", filename);
		printf("\nPlease check that the specified file exists.");
		getch();
		exit(1);
	}
	while (fgets(line, 81, inFile) != NULL)
	printf("\n%s", line);
	for(i = 0; line[i]; ++i)
        {
			line[i] = toupper(line[i]);
			
        }
	printf("\n\nPrinted in UPPER case: \n\n");
	puts(line);
        
	getch();

}



Also i didnt mean to include

double prices;



in the code above. Sorry about that.

Is This A Good Question/Topic? 0
  • +

Replies To: converting string array to uppercase

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1353
  • View blog
  • Posts: 4,660
  • Joined: 19-February 09

Re: converting string array to uppercase

Posted 19 April 2013 - 07:17 PM

Hi, you read each line into the line array, which is overwritten each time. So you only remember one line, which is why the last line is the only one left.

If you need to print the original version of the file, you can print it as you are. Then use rewind to go to the start of the file and print the capitalized version.
Was This Post Helpful? 0
  • +
  • -

#3 thesyntaxdr  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 19-April 13

Re: converting string array to uppercase

Posted 19 April 2013 - 08:00 PM

Ok i am still stuck. I understand what you are saying and i added the rewind but i am still not quite sure how to make sure that i display the capitalized version. below is the updated code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

int main()
{
	char line[81], filename[21], ch;
	int i;
	FILE *inFile;
	
	printf("\nPlease enter the name of a file: ");
	gets(filename);
	inFile = fopen(filename, "r");
	if (inFile == NULL)
	{
		printf("\nThe file %s was not successfully opened.", filename);
		printf("\nPlease check that the specified file exists.");
		getch();
		exit(1);
	}

	while (fgets(line, 81, inFile) != NULL)
	printf("\n%s\n", line);
	for(i = 0; line[i]; ++i)
        {
			line[i] = toupper(line[i]);
        }

	printf("\nPrinted in UPPER case: \n\n");
	rewind(inFile);
	while (fgets(line, 81, inFile) != NULL)
	printf("\n%s", line);
	getch();

}



i added the rewind and i am stuck on what to do after that.

*** EDIT ***
No need to quote post above your reply

This post has been edited by GunnerInc: 19 April 2013 - 11:02 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: converting string array to uppercase

Posted 19 April 2013 - 08:07 PM

First thing is first, NEVER USE GETS
gets(filename);



This function is so dangerous that even the compiler will warn you about using it. Instead use fgets, which will let limit the amount of input.

With that said, since you just dealing with individual characters, you should just read a character at a time from the file and then process it.

i.e.
/* read char by char until end of file */
while((c = fgetc(inFile)) != EOF) {
   if(/* c is a alpha character and c is lower case */) {
      /* set c to uppercase */
   /* print c */
}


This post has been edited by jjl: 19 April 2013 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1