[ back to toc ]

a very basic question from a beginner...

Date: 2002/05/28 10:37

Q:
Hi, this might be pretty basic, but I am trying to solve the following
problem for my C Programming class, and your help would be much
appreciated. I could not solve the problem with my current C skills or by
using my book/web-search. Please bare with me…:-) and thanks for helping
me out. Here is the problem:

1. Write a function void shift(char *) that looks at the first character
of its argument and converts the other characters, to be the same case,
that is, if the first character is uppercase, it shifts the rest to
uppercase; if the first character is lowercase, it shifts the rest to
lowercase, and otherwise, it does nothing.

So, I've written the program with a function that should perform this
task, but my 2 major problems are:

1. How do I capture the value of the output string (from my function) in
main? I am getting all error messages for line 17 I guess: (please see my
code below)

Line 17: output_data=(shift(entered_data));

I think this is not valid, as I cannot use the assignment operator with a
string. What should I do then?

2. The actual problem asks for a void shift(char*) function, which means,
I should re-write my code (below) using pointer notation, but this is very
confusing for a newbie like me. Can you please give me some advice on that
as well?

Thanks,

Here's my attempt…

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

void shift(char name[]);

int main(void)
{
char entered_data[100];
char output_data[100];

int i=0;

printf("Enter your name:\n");
gets(entered_data);

output_data=(shift(entered_data)); //I cannot make this work

puts(output_data);

return 0;
}

void shift(char name[]) /*I know this should be working, as if I use it in
main it works… I also need to have this in pointer notation*/

{
int i=0;

if (isupper(name[0])==1)
for (i=0;i<100;i++)
name[i]=toupper(name[i]);

else
for (i=0;i<100;i++)
name[i]=tolower(name[i]);

return name;
}

A:
void shift(char *name)

works on the string that you pass as argument. You actually do NOT pass
the string as an argument. Rather the string is somewhere in memory and
the function gets the pointer to the string. After that the function works
on the string itself, and alters the original string.

void shift does not return anything therefore you can not assigned the
returned value to output_data, because there is no returned value. What is
more output_data declared as

char output_data[100];

is constant. 'output_data' actually (and sloppily saying) is the address,
where the compiler reserved 100 bytes to store characters. You can not
assign a new value to this 'output_data' as this is constant.

What you try to achieve is to copy the characters (altered) to the
output_data. This can be done calling the function

strcpy

or

memcpy

Finally: your function void shift(char *name)

void shift(char *name){
if( isupper(*name) )
while( *name ){
if( islower(*name) )*name = toupper(*name);
name++;
}
else
while( *name ){
if( isupper(*name) )*name = tolower(*name);
name++;
}
return;
}

To use this function all you need is:

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

void shift(char *name);

int main(void)
{
char entered_data[100];
printf("Enter your name:\n");
gets(entered_data);
shift(entered_data);
puts(entered_data);
}

Regards,
Peter

[ back to toc ]