I am trying to print the first letter of a word at the position entered by the user. If there is a whitespace, then it will just stop searching. There is a warming message concerning funtion gets()
. I have problems with the pointers and funtion, it just does not return the letter. I think the porblem might be on the while
loop. Function firstLetter
prints the first letter of a word. For instance, if the user enters index 5 of a previously entered string, and in str[5] corresponds to the 'e' of the word 'ice-cream' then the firstLetter
will search for the first letter of that word, in this case it will return 'i'.
#include <stdio.h>
#include <string.h>
int firstLetter(int , char *);
int main()
{
char str[200];
printf("Insert line: ");
gets(str);
int pstn;
scanf("%i,&pstn");
firstLetter(pstn, str);
return 0;
}
int firstLetter(int i, char *str)
{
if(str[i]=' ')
{
printf("No letters at this position");
}
else
{
while(str[i]!=' ')
{
i--;
}
printf("First letter: %c ",str[i 1]);
}
return 0;
}
CodePudding user response:
For starters in this line
scanf("%i,&pstn");
you have a typo. At least you need to rewrite it like
scanf("%i",&pstn);
Also within the function firstLetter
there is also a typo in the if statement
if(str[i]=' ')
where you are using the assignment operator =
instead of the comparison operator ==
.
The function gets
is unsafe and is not supported by the C Standard.
Instead use the function fgets
fgets( str, sizeof( str ), stdin );
The variable pstn
should have the unsigned integer type size_t
. Otherwise the user can enter a negative number.
Also you need to check that the entered position is not greater than the length of the string.
For example
size_t pstn = 0;
scanf("%zu", &pstn);
if ( pstn < strlen( str ) ) firstLetter(pstn, str);
The function parameter should have the qualifier const because the passed string is not changed within the function
Also the function has a bug because in this while loop
while(str[i]!=' ')
{
i--;
}
there is no check whether i
is less than 0
.
Also the return type int
of the function is useless.
The function can be declared and defined the following way
void firstLetter( const char *str, size_t i )
{
if ( str[i] == ' ' )
{
printf("No letters at this position");
}
else
{
while( i != 0 && str[i-1] != ' ' )
{
i--;
}
printf("First letter: %c\n", str[i] );
}
}
So in main the function is called like
size_t pstn = 0;
scanf("%zu", &pstn);
if ( pstn < strlen( str ) ) firstLetter( str, pstn );
Or you can make the check that the specified position in the string is less than the length of the string within the function as for example
void firstLetter( const char *str, size_t i )
{
if ( !( i < strlen( str ) ) || str[i] == ' ' )
{
printf("No letters at this position");
}
else
{
while( i != 0 && str[i-1] != ' ' )
{
i--;
}
printf("First letter: %c\n", str[i] );
}
}
In this case the function is called like
size_t pstn = 0;
scanf("%zu", &pstn);
firstLetter( str, pstn );
Instead of the comparison with the space character ' '
you could compare with the space character ' '
and the tab character '\t'
using the standard C function isblank
declared in the header <ctype.h>
. For example
#include <string.h>
#include <ctype.h>
void firstLetter( const char *str, size_t i )
{
if ( !( i < strlen( str ) ) || isblank( ( unsigned char )str[i] ) )
{
printf("No letters at this position");
}
else
{
while( i != 0 && !isblank( ( unsigned char )str[i-1] ) )
{
i--;
}
printf("First letter: %c\n", str[i] );
}
}