1
I’m trying to make a program that reads a sentence and then capitalizes every initial of every word, if not already. The problem is that I type in a sentence but it only returns the first word, nothing else. the word is with the first letter capitalized but the other words of the sentence do not appear in the output.
#include<stdio.h>
#include<ctype.h>
char namechange( char abc[], int size);
int main()
{
int i,n;
char name[100000];
scanf("%s", &name);
namechange( name, n );
}
char namechange( char abc[], int size)
{
int i, k = 0;
for ( i = 0; abc[i] != '\0'; i ++)
{
int a,b;
a = abc[i];
b = abc[i - 1];
if (i == 0 || 'b' == 8)
abc[i] = toupper (abc[i]);
}
while ( abc[k] != '\0')
{
printf("%c", abc[k]);
k = k + 1;
}
}
Thanks for the help, I did not understand the change ( formatting ) you made in scanf. I should use fgets preferably?
– Vitor Matos
The
fgets()
is used in real applications,scanf()
only for quick exercises or if it makes a lot of sense, which is rare.– Maniero
The
scanf
is often used to make the times ofstrtol()
, to read and parse numbers, but for strings it is very deficient.– Wtrmute
@Wtrmute the
scanf()
not because he only readsstdin
.– Maniero
True. Properly it would be the
fscanf()
and thesscanf()
; but thescanf()
ends up being used in cases where the string would come fromstdin
, as in the example, but also in Unix-like applications that readstdin
and writestdout
.– Wtrmute
scanf("%[ n]s", name)?
– Maurício Z.B