- #1
- 4,652
- 37
This is the latest assignment I am struggling with:
Write a program that contains 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.
I have written a program that will take a string and do the conversion, but I can't figure out how to wrap up the "test the first character and do the conversion" functionality into a function and make it work by only passing the pointer variable as an argument. I thought I would need to pass the whole array in order for isupper() to do the test on the first character. Or maybe this void shift(char *) syntax actually does pass the whole array - I am so clueless!
Is the void shift(char *) heading all I need to take the argument? I was thinking I would need to use some kind of temp variable void shift(char *x) in order to assign the value of the argument somewhere and then use it inside my function.
Here's what I have been able to do so far. Thanks for your advice. Sorry for the format, I am having a little trouble indenting.
# include <stdio.h>
# include <ctype.h>
int main(void)
{
char str[80], *p; // declares a string of 80 chars and a pointer called p
printf("Enter a string \n");
gets(str); //get the entered string
p = str; //pointer gets the string address
while(*p) //until a null value is found
{
if (isupper(str[0])) //determine if first char in array is uppercase
{
*p = toupper(*p); //convert them all to upper case
p++;
}
else
{
*p = tolower(*p); //otherwise make all chars lowercase
p++;
}
}
printf("%s \n",str);
return 0;
}
Write a program that contains 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.
I have written a program that will take a string and do the conversion, but I can't figure out how to wrap up the "test the first character and do the conversion" functionality into a function and make it work by only passing the pointer variable as an argument. I thought I would need to pass the whole array in order for isupper() to do the test on the first character. Or maybe this void shift(char *) syntax actually does pass the whole array - I am so clueless!
Is the void shift(char *) heading all I need to take the argument? I was thinking I would need to use some kind of temp variable void shift(char *x) in order to assign the value of the argument somewhere and then use it inside my function.
Here's what I have been able to do so far. Thanks for your advice. Sorry for the format, I am having a little trouble indenting.
# include <stdio.h>
# include <ctype.h>
int main(void)
{
char str[80], *p; // declares a string of 80 chars and a pointer called p
printf("Enter a string \n");
gets(str); //get the entered string
p = str; //pointer gets the string address
while(*p) //until a null value is found
{
if (isupper(str[0])) //determine if first char in array is uppercase
{
*p = toupper(*p); //convert them all to upper case
p++;
}
else
{
*p = tolower(*p); //otherwise make all chars lowercase
p++;
}
}
printf("%s \n",str);
return 0;
}