Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,961 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,567 people online right now. Registration is fast and FREE... Join Now!




char arrays and function gets

 
Reply to this topicStart new topic

char arrays and function gets, char arrays and function gets

biddy
25 Feb, 2007 - 07:06 AM
Post #1

New D.I.C Head
*

Joined: 26 Jan, 2007
Posts: 12


My Contributions
HI all

I am trying to write a program that inputs a line of text with function gets into char adday s[100] and outputs the line in uppercase and lowercase letters.

Can anyone please advise if this looks correct? I cannot seem to get my compiler working today...........
CODE

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

void main ()
{
char linearray s[ 100 ]; /* create char array */

printf( ''Enter a  line of text:\n'' );

/* use gets to read line of text*/
gets ( linearray s);

printf( ''%s%c\n, ''The line converted to uppercase is  '', toupper( ' line ');

printf( ''%s%c\n, '' The line converted to lowercase is '', tolower ('line');

return 0; /* indicates successful termination */

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Char Arrays And Function Gets
25 Feb, 2007 - 07:11 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,225



Thanked: 36 times
Dream Kudos: 25
My Contributions
There may be a few issues - please see the comments in the code:
CODE

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

void main () comment: C standards indicate that retun type of the main function should be of type int
{
char linearray s[ 100 ]; /* create char array */ comment: you have whitespace in the variable name - not allowed. Is the variable name linearray or s?

printf( ''Enter a line of text:\n'' );

/* use gets to read line of text*/
gets ( linearray s); comment:  same problem as above, one variable name or the other.

printf( ''%s%c\n, ''The line converted to uppercase is '', toupper( ' line ');
comment: The brackets for the printf() function are not closed. the toupper function takes a single char, not an entire string, you will have to iterate through it, finally you have no variable named line, and if one did, it should not be encased in quotes.

printf( ''%s%c\n, '' The line converted to lowercase is '', tolower ('line');
comment: The brackets for the printf() function are not closed. the toupper function takes a single char, not an entire string, you will have to iterate through it, finally you have no variable named line, and if one did, it should not be encased in quotes.

return 0; /* indicates successful termination */
comment: you have not provided a closing brace for the main function

User is online!Profile CardPM
+Quote Post

biddy
RE: Char Arrays And Function Gets
25 Feb, 2007 - 07:24 AM
Post #3

New D.I.C Head
*

Joined: 26 Jan, 2007
Posts: 12


My Contributions
Thanks for you help. I have changed some items but have a few more querieis on this........per the question i think the name of the array is s........so I've changed that. Not sure how to iterate through.........do you mean I need a for loop or something? Can you take another look? Thanks.

CODE

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

int main void
{
char array s[ 100 ]; /* create char array */

printf( ''Enter a  line of text:\n'' );

/* use gets to read line of text*/
gets ( array s);

printf( ''%s%c\n, ''The line converted to uppercase is  '', toupper );

printf( ''%s%c\n, '' The line converted to lowercase is '', tolower );

return 0; /* indicates successful termination */

} /* end main */

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Char Arrays And Function Gets
25 Feb, 2007 - 07:31 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,225



Thanked: 36 times
Dream Kudos: 25
My Contributions
See comments in code again:
CODE

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

int main void
{
char array s[ 100 ]; /* create char array */ comment:  again, you have tow variable names, it will need to look like the following: char s[100];

printf( ''Enter a line of text:\n'' );

/* use gets to read line of text*/
gets ( array s); comment: same as above, will need to resemble gets(s);

printf( ''%s%c\n, ''The line converted to uppercase is '', toupper ); comment: toupper is a function that takes a single char as a parameter, you will need to use a looping mechanism to talk through each element of the array and apply the function to each element.

printf( ''%s%c\n, '' The line converted to lowercase is '', tolower );
comment: lower is a function that takes a single char as a parameter, you will need to use a looping mechanism to talk through each element of the array and apply the function to each element.

return 0; /* indicates successful termination */

} /* end main */

See below for an example of walking through the array and applying the function:
CODE

int main(int argc, char *argv[])
{
  char str1[256];
  int i;
  printf("Please enter a string:\n");
  gets(str1);
  for(i=0;i<strlen(str1);i++)
     str1[i]=toupper(str1[i]);
  printf("The upper string is: %s\n",str1);
  for(i=0;i<strlen(str1);i++)
     str1[i]=tolower(str1[i]);
  printf("The lower string is: %s\n",str1);
  return 0;
}

User is online!Profile CardPM
+Quote Post

biddy
RE: Char Arrays And Function Gets
25 Feb, 2007 - 07:41 AM
Post #5

New D.I.C Head
*

Joined: 26 Jan, 2007
Posts: 12


My Contributions
Thanks ..just to confirm the below is correct?
CODE

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

int main(int argc, char *argv[])
{
  char s[100];
  int i;
  printf("Please enter a string:\n");
  gets(s);
  for(i=0;i<strlen(s);i++)
     s[i]=toupper(s[i]);
  printf("The upper string is: %s\n",s);
  for(i=0;i<strlen(s);i++)
     s[i]=tolower(s[i]);
  printf("The lower string is: %s\n",s);
  return 0;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Char Arrays And Function Gets
25 Feb, 2007 - 07:43 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,225



Thanked: 36 times
Dream Kudos: 25
My Contributions
Well, as that is the code I'd given, yes it is correct, although you will need to include the string.h header in order to use the strlen() function.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:28AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month