P
ptq2238
Hi,
Tried this code to assist my understanding of strings and functions
but I'm not sure why the errors are occurring and hope someone can
shed some light to my learning.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define STRINGSIZE 10
char isLowercase(char *string, char *string2);
int main()
{
char string1[STRINGSIZE];
char string2[STRINGSIZE];
printf("Please enter a string for checking: ");
fgets(string1, STRINGSIZE, stdin);
isLowercase(string1, string2);
printf("Main String1 = %s",string1);
printf("Main String2 = %s",string2);
return 0;
}
char isLowercase(char *string1, char *string2)
{
/* copies lowercase elements from string1 to string2. */
int count1 = 0;
int count2 = 0;
while (string1[count1] != '\0')
{
if (islower((int) string1[count1]))
{
string2[count2] = string1[count1];
count1++;
count2++;
}
else
{
count1++;
}
}
string2[count2 + 1] = '\0';
printf("isLower String2 = %s\n",string2);
return;
}
When I compile, gcc -Wall gave me this error, even though I thought
that I'm returning string2.
copy_elements.c: In function `isLowercase':
copy_elements.c:51: warning: `return' with no value, in function
returning non-void
I tried return string2; but I don't understand the warning gcc gives:
cp_elements.c: In function `isLowercase':
cp_elements.c:51: warning: return makes integer from pointer without a
cast
So I tried type cast my return string but this didn't help.
I tried running the program and I get the following result.
Please enter a string for checking: QwerT12
isLower String2 = wer
Main String1 = QwerT12
While it does find the correct lower case letters, isLower String2 =
wer, it would appear that the string is not being pased back to main.
Why I don't see a result back in Main, ie "Main String2" at all ?
Thank you.
Pat
Tried this code to assist my understanding of strings and functions
but I'm not sure why the errors are occurring and hope someone can
shed some light to my learning.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define STRINGSIZE 10
char isLowercase(char *string, char *string2);
int main()
{
char string1[STRINGSIZE];
char string2[STRINGSIZE];
printf("Please enter a string for checking: ");
fgets(string1, STRINGSIZE, stdin);
isLowercase(string1, string2);
printf("Main String1 = %s",string1);
printf("Main String2 = %s",string2);
return 0;
}
char isLowercase(char *string1, char *string2)
{
/* copies lowercase elements from string1 to string2. */
int count1 = 0;
int count2 = 0;
while (string1[count1] != '\0')
{
if (islower((int) string1[count1]))
{
string2[count2] = string1[count1];
count1++;
count2++;
}
else
{
count1++;
}
}
string2[count2 + 1] = '\0';
printf("isLower String2 = %s\n",string2);
return;
}
When I compile, gcc -Wall gave me this error, even though I thought
that I'm returning string2.
copy_elements.c: In function `isLowercase':
copy_elements.c:51: warning: `return' with no value, in function
returning non-void
I tried return string2; but I don't understand the warning gcc gives:
cp_elements.c: In function `isLowercase':
cp_elements.c:51: warning: return makes integer from pointer without a
cast
So I tried type cast my return string but this didn't help.
I tried running the program and I get the following result.
Please enter a string for checking: QwerT12
isLower String2 = wer
Main String1 = QwerT12
While it does find the correct lower case letters, isLower String2 =
wer, it would appear that the string is not being pased back to main.
Why I don't see a result back in Main, ie "Main String2" at all ?
Thank you.
Pat