Reputation: 1533
I am trying to write a function that gets a string of letters, either capital letters or small letters, and prints 2 other strings, one with only the capitals, and one only with the small letters. for example:
input: AaBbCcDD
Output: Capital string is ABCDD, non capital is abc
My code is not working correctly, it seems to skip over the last letter. To test it, I wrote the following code:
int length;
printf("Please enter length of string\n");
scanf("%d",&length);
string=create_string(length);
scan_string(string,length);
printf("The string entered is: \n");
print_string(string,length);
Where create_string is:
char* create_string(int size)
{
char* string;
string=(char*)malloc(size*sizeof(char));
return string;
}
Scan string is:
void scan_string(char* string, int size)
{
int i;
printf("Please enter %d characters\n",size);
for(i=0;i<size;i++)
scanf("%c",string+i);
}
And print string is
void print_string(char* string,int size)
{
int i;
for(i=0;i<size;i++)
printf("%c ",*(string+i));
}
When I try even just to print the string I entered, this is what I get, after I input aaAAB
The output is a a A A
.
it skipped over the B.
Upvotes: 1
Views: 228
Reputation: 211
The problem is because the scanf
does not eat the "\n"
. Hence there is still one '\n'
remaining at your first input. This will be counted at the next scanf.
Try to put an additional getchar()
right after your first scanf
.
printf("Please enter length of string\n");
scanf("%d",&length);
getchar(); // remove '\n'
string=create_string(length);
Upvotes: 0
Reputation: 4433
I think your code is unnecessarily elaborated. To read a string the function fget()
with parameter stdin
is a simpler choice.
For example, I wuold not ask to the user for the length of the string.
Perhaps it is better to use a buffer with fixed length, and to restrit the user to enter a string with the length less than which you have been previously stipulated.
#define MAXLEN 1000
char buffer[MAXLEN] = "";
fgets(buffer, MAXLEN, stdin);
If the user attempts to enter a string with more than MAXLEN characters, it would be necessary to handle the end-of-line in some way, but I think this is out of topic.
So, in general, let us suppose that MAXLEN is large enough such that buffer contains the \n
mark.
Now, a call to your function print_string()
can be done.
However, it would be better to do this:
printf("%s", buffer);
I think that you probably need to take in account the C convention for strings: a string is a char array whose last element is marked with the character '\0'
(null character, having always code 0).
Even if you want to insist in your approach, I think that scanf()
is a bad choice to read individual characters. it is more easy to use getchar()
, instead.
By using scanf()
you have to broke your brain figurating out all the stuff around the behaviour of scanf()
, or how to handle the read of characters, and so on.
However, getchar()
reads one char at a time, and that's (almost) all. (Actually, the console commonly not returns the control to the user until an end-of-line \n
has been read).
string[i] = getchar();
Upvotes: 0
Reputation: 41
You can change your first scanf to read '\n' as below. This will read the extra '\n'
scanf("%d\n", &length);
Upvotes: 0
Reputation: 727067
The problem is with the scanf
that reads characters using %c
: it follows the scanf
that reads the length using %d
, which leaves an extra '\n'
character in the buffer before the first character that you get.
If you modify the output to put quotes around your characters, you would actually see the \n
:
void print_string(char* string,int size)
{
int i;
for(i=0;i<size;i++)
printf("'%c' ",*(string+i));
}
This prints
'
' 'a' 'a' 'A' 'A'
Upvotes: 2