Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #73834 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-10-26 03:09 -0700 |
| Last post | 2015-10-26 10:43 -0700 |
| Articles | 15 on this page of 115 — 21 participants |
Back to article view | Back to comp.lang.c
Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 03:09 -0700
Re: Program to output initials of a name Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-26 03:22 -0700
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 05:32 -0700
Re: Program to output initials of a name Barry Schwarz <schwarzb@dqel.com> - 2015-10-26 14:34 -0700
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 10:38 +0000
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 05:37 -0700
Re: Program to output initials of a name Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 13:53 +0000
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 07:33 -0700
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 15:21 +0000
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-26 16:26 +0000
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 16:41 +0000
Re: Program to output initials of a name Geoff <geoff@invalid.invalid> - 2015-10-26 10:27 -0700
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-26 17:29 +0000
Re: Program to output initials of a name raltbos@xs4all.nl (Richard Bos) - 2015-11-10 15:40 +0000
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-27 13:36 +0000
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 13:45 +0000
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-27 15:10 +0100
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-27 20:08 +0000
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-27 22:28 +0100
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-27 21:34 +0000
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-28 08:50 +0100
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-28 15:19 +0000
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-28 22:17 +0100
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-29 19:54 +0000
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-30 08:44 +0100
Re: Program to output initials of a name James Kuyper <jameskuyper@verizon.net> - 2015-10-30 13:44 -0400
Re: Program to output initials of a name guinness.tony@gmail.com - 2015-10-28 04:19 -0700
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-28 12:50 +0100
Re: Program to output initials of a name guinness.tony@gmail.com - 2015-10-28 08:19 -0700
Re: Program to output initials of a name guinness.tony@gmail.com - 2015-10-28 10:21 -0700
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-28 22:34 +0100
Re: Program to output initials of a name raltbos@xs4all.nl (Richard Bos) - 2015-11-10 15:50 +0000
Re: Program to output initials of a name Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-27 14:13 +0000
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 14:15 +0000
Re: Program to output initials of a name Keith Thompson <kst-u@mib.org> - 2015-10-27 08:59 -0700
Re: Program to output initials of a name Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-27 21:34 +0000
Re: Program to output initials of a name James Kuyper <jameskuyper@verizon.net> - 2015-10-27 12:09 -0400
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-27 20:03 +0000
Re: Program to output initials of a name James Kuyper <jameskuyper@verizon.net> - 2015-10-27 19:36 -0400
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-28 09:06 +0100
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-28 12:28 +0000
Re: Program to output initials of a name Geoff <geoff@invalid.invalid> - 2015-10-26 10:15 -0700
Re: Program to output initials of a name Ian Collins <ian-news@hotmail.com> - 2015-10-27 07:34 +1300
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 18:40 +0000
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-26 19:00 +0000
Re: Program to output initials of a name Ian Collins <ian-news@hotmail.com> - 2015-10-27 08:53 +1300
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-26 20:41 +0000
Re: Program to output initials of a name Ian Collins <ian-news@hotmail.com> - 2015-10-27 10:24 +1300
Re: Program to output initials of a name Keith Thompson <kst-u@mib.org> - 2015-10-26 16:06 -0700
Re: Program to output initials of a name Ian Collins <ian-news@hotmail.com> - 2015-10-27 13:16 +1300
Re: Program to output initials of a name Keith Thompson <kst-u@mib.org> - 2015-10-27 08:46 -0700
Re: Program to output initials of a name Ian Collins <ian-news@hotmail.com> - 2015-10-28 07:25 +1300
Re: Program to output initials of a name raltbos@xs4all.nl (Richard Bos) - 2015-11-10 15:52 +0000
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 02:15 +0000
Re: Program to output initials of a name Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-26 19:28 -0700
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 02:39 +0000
Re: Program to output initials of a name Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-27 02:59 -0700
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 10:24 +0000
Re: Program to output initials of a name Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-27 03:57 -0700
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 11:09 +0000
Re: Program to output initials of a name Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-27 11:23 +0000
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 11:28 +0000
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-27 11:37 +0000
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 11:46 +0000
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-27 11:50 +0000
Re: Program to output initials of a name supercat@casperkitty.com - 2015-10-27 08:44 -0700
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-27 11:19 +0000
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-27 11:25 +0000
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-28 14:24 +0000
Re: Program to output initials of a name James Kuyper <jameskuyper@verizon.net> - 2015-10-26 14:00 -0400
Re: Program to output initials of a name Ian Collins <ian-news@hotmail.com> - 2015-10-27 11:00 +1300
Re: Program to output initials of a name Lowell Gilbert <lgusenet@be-well.ilk.org> - 2015-10-27 15:39 -0400
Re: Program to output initials of a name Keith Thompson <kst-u@mib.org> - 2015-10-26 10:17 -0700
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 10:28 -0700
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-26 10:43 +0000
Re: Program to output initials of a name Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-26 03:52 -0700
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-27 13:11 +0000
Re: Program to output initials of a name David Brown <david.brown@hesbynett.no> - 2015-10-27 15:14 +0100
[OT] family naming conventions James Kuyper <jameskuyper@verizon.net> - 2015-10-27 12:01 -0400
Re: [OT] family naming conventions Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-10-27 13:17 -0600
Re: [OT] family naming conventions Siri Cruz <chine.bleu@yahoo.com> - 2015-10-27 13:55 -0700
Re: [OT] family naming conventions Robert Wessel <robertwessel2@yahoo.com> - 2015-10-27 17:59 -0500
Re: [OT] family naming conventions raltbos@xs4all.nl (Richard Bos) - 2015-11-10 15:56 +0000
Re: [OT] family naming conventions James Kuyper <jameskuyper@verizon.net> - 2015-11-10 11:41 -0500
Re: [OT] family naming conventions raltbos@xs4all.nl (Richard Bos) - 2015-11-10 19:00 +0000
Re: [OT] family naming conventions James Kuyper <jameskuyper@verizon.net> - 2015-11-10 14:11 -0500
Re: [OT] family naming conventions raltbos@xs4all.nl (Richard Bos) - 2015-11-10 21:19 +0000
Re: [OT] family naming conventions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-10 13:32 -0800
Re: [OT] family naming conventions raltbos@xs4all.nl (Richard Bos) - 2015-11-13 21:38 +0000
Re: [OT] family naming conventions Udyant Wig <udyantw@gmail.com> - 2015-11-11 15:47 +0530
Re: [OT] family naming conventions raltbos@xs4all.nl (Richard Bos) - 2015-11-13 21:36 +0000
Re: Program to output initials of a name Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-27 08:50 -0700
Re: Program to output initials of a name Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-27 16:23 +0000
Re: Program to output initials of a name Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-10-27 10:06 -0700
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 05:40 -0700
Re: Program to output initials of a name Udyant Wig <udyantw@gmail.com> - 2015-10-26 16:20 +0530
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-26 10:55 +0000
Re: Program to output initials of a name Keith Thompson <kst-u@mib.org> - 2015-10-26 10:20 -0700
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-26 17:36 +0000
Re: Program to output initials of a name Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-28 16:54 -0700
Re: Program to output initials of a name Bartc <bc@freeuk.com> - 2015-10-29 00:50 +0000
Re: Program to output initials of a name Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-30 08:18 -0700
Re: Program to output initials of a name Barry Schwarz <schwarzb@dqel.com> - 2015-10-26 06:15 -0700
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 09:42 -0700
Re: Program to output initials of a name Keith Thompson <kst-u@mib.org> - 2015-10-26 11:20 -0700
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 09:44 -0700
Re: Program to output initials of a name Keith Thompson <kst-u@mib.org> - 2015-10-26 11:21 -0700
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 09:47 -0700
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 10:47 -0700
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 18:20 +0000
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 11:56 -0700
Re: Program to output initials of a name Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-26 19:24 +0000
Re: Program to output initials of a name Alla _ <modelling.data@gmail.com> - 2015-10-26 12:01 -0700
Re: Program to output initials of a name Richard Heathfield <rjh@cpax.org.uk> - 2015-10-26 17:48 +0000
Re: Program to output initials of a name Keith Thompson <kst-u@mib.org> - 2015-10-26 10:43 -0700
Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-10-29 00:50 +0000 |
| Message-ID | <n0rqd1$tpa$1@dont-email.me> |
| In reply to | #74120 |
On 28/10/2015 23:54, Tim Rentsch wrote: > Bartc <bc@freeuk.com> writes: > >> On 26/10/2015 17:20, Keith Thompson wrote: >>> Some compilers will shut up if you add an extra set of parentheses: >>> >>> if ((user_name[i] = space)) >>> >>> (Of course do this only if you really want to use the result of the >>> assignment as a condition.) >> >> Out of the compilers I use, Clang, will do that. But I haven't found a >> way of silencing DMC. > > What happens if you try > > if ( user_name[i] = space, user_name[i] ) ... Then they're both silent. But the result of the assignment isn't used as a conditional expression, which must be what triggers the error. (If you're suggesting that construct to quieten the messages, that's probably going to much out of your way do to so.) -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <txr@alumni.caltech.edu> |
|---|---|
| Date | 2015-10-30 08:18 -0700 |
| Message-ID | <kfnio5o9yh1.fsf@x-alumni2.alumni.caltech.edu> |
| In reply to | #74125 |
Bartc <bc@freeuk.com> writes: > On 28/10/2015 23:54, Tim Rentsch wrote: >> Bartc <bc@freeuk.com> writes: >> >>> On 26/10/2015 17:20, Keith Thompson wrote: >>> >>>> Some compilers will shut up if you add an extra set of parentheses: >>>> >>>> if ((user_name[i] = space)) >>>> >>>> (Of course do this only if you really want to use the result of the >>>> assignment as a condition.) >>> >>> Out of the compilers I use, Clang, will do that. But I haven't found a >>> way of silencing DMC. >> >> What happens if you try >> >> if ( user_name[i] = space, user_name[i] ) ... > > Then they're both silent. But the result of the assignment isn't used > as a conditional expression, which must be what triggers the error. > > (If you're suggesting that construct to quieten the messages, that's > probably going to much out of your way do to so.) I'm not suggesting anything. I do think anyone reading it would draw the obvious conclusion that an assignment and then a test of the assigned value is what the author intended. That it happens to get rid of an unwanted warning message is just a fortuitous side benefit.
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-10-26 06:15 -0700 |
| Message-ID | <jn7s2b1a2fjisckod30eunb2itk1dv5osb@4ax.com> |
| In reply to | #73834 |
On Mon, 26 Oct 2015 03:09:48 -0700 (PDT), Alla _
<modelling.data@gmail.com> wrote:
>Hello!
>
>Below I am posting a first version of a program
>that should output initials of a user's name
>in uppercase letters, without spaces.
>
>Please, do me a favor and take a look at this
>first version, which is a truncated version of
>the initial program I wrote.
>
>The initial version doesn't work, therefore
>to find what is wrong, I have broken the
>program into few simpler versions.
>
>I would be grateful if, for now, you do not add additional
>suggestions on how to do this task, and help me
>only within this first, the simplest, version without
>adding more complicated features into it.
>
>If I understand my mistakes in this one, I will try to
>figure out my mistakes in fuller version, and post questions
>later.
>
>I am testing it without 'prompt-the-user' feature.
>
>I read this program the following way:
>
>- the name array is Alla;
If you want the program to print the name's initials, don't you think
you should test with a name that has more than one?
>- length of the name array is 4;
>
>- print the first letter in uppercase (as
>it if already in the upper case, print as
>it is);
It is true that if the first character is upper case then it is
printed as is. However, there is nothing in your code to accomplish
this. In all cases you call toupper. It just so happens that for an
upper case letter toupper returns the same value it receives. If you
were printing the square root of a number, would your comment say
"print the square root but if the number is 1 print the number as is"?
>- for loop starts from the letter 'l' (i = 1)
>and scans the whole name to see if there are
>any spaces between letters;
>
>- there are no spaces, hence only one letter
>should be printed out;
>
>- in case there were spaces, after printing
>the first letter (printed in any case), the program
>prints out every letter after the space, i.e. name[i+1].
Actually, it prints only the first character after a space.
>Why does the program print 'ALA' as the outcome, if
>there are no spaces in the word Alla?
Others have identified the coding error in your if statement.
>/**
> * Program to print initials of the name
>**/
>
>#include <stdio.h>
>#include <string.h>
>
>void toupper_initials(char user_name[]);
>
>int main(void)
>{
> char name[] = "Alla";
>
> toupper_initials(name);
>
> return 0;
>}
>
>/** Function to print initials in uppercase letters
> without spaces
> **/
>
>void toupper_initials(char user_name[])
>{
> char initials[80];
What purpose does initials serve? While you store data in the array,
you never use it. The only "real" action you take with an initial is
to print it (in upper case).
> char space = ' ';
> int len = strlen(user_name);
> int i;
>
> printf("%c", toupper(user_name[0]));
>
> for (i = 1; i < len; i++) /** also can be while(user_name[i] != '\0') **/
You cannot simply replace the for statement with a while statement. If
you chose the while statement, you would have to add something to
increment i.
> {
>
> /** if the user has entered several names (divided by spaces),
> then convert to upper case each letter after the space in the
> user_name array; if the letter is in upper case, toupper prints
> the letter as it is
> **/
toupper does not print anything, ever.
A language nit: "... then convert to upper case the letter after each
space ..."
> if (user_name[i] = space)
> {
> initials[i] = toupper(user_name[i + 1]);
>
> printf("%c", initials[i]);
> }
> }
> /** if there are no spaces between letters, the for loop terminates
> after scanning the whole string, and should produce no outcome;
> thus the program prints out only the first letter in upper case **/
Another language nit: "The for loop terminates ... string. If there
are no spaces, it will produce no output. Thus the program ..."
> printf("\n");
>}
>
>Thank you!
>
>
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-10-26 09:42 -0700 |
| Message-ID | <329ed0a1-218f-4e18-b85d-2f749fb3d218@googlegroups.com> |
| In reply to | #73834 |
Moving forward with the function part of the program.
I am trying to make the program take into account possible
spaces, including spaces at initial places of an array.
I stumbled upon using the outside for loop, inner while loop,
and Boolean expression.
Goal: if user_name[i] is space, then the program should
increment i until non-space character is found, and print out
only this character, skipping all the rest. Then, if another
space is found, it does the same.
If there are no spaces, including no spaces in the beginning
(which should also be considered above), the program prints out
only one initial letter [0].
I have a few versions, but none would work. I even see why,
but it's been two hours (two shameful hours) that I can't
come up with anything decent.
(skipping the name of the function, and initial declarations)
1 one:
/** I think this version will print out all letters in the
upper case format, once it encounters them. But I need only
the first letter **/
for (i = 0; i < len; i++)
{
if (user_name[i] == space)
continue;
else
printf("%c", toupper(user_name[i])
}
2 one:
void toupper_initials(char user_name[])
{
char space = ' ';
bool is_space = 0;
int len = strlen(user_name);
int i;
for (i = 0; i < len; )
{
if (is_space)
{
while (user_name[i] == space)
user_name[i++];
is_space = 0; /** I guess short version 'is_space;' would be suffice because 'is_space;' will mean is_space is 0 as set initially **/
printf("%c", toupper(user_name[i]));
/** this should print an i-th character if it is not a space, but
}
else // there are no spaces in the beginning
{
printf("%c", toupper(user_name[0]));
}
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-26 11:20 -0700 |
| Message-ID | <ln4mhdfq4r.fsf@kst-u.example.com> |
| In reply to | #73869 |
Alla _ <modelling.data@gmail.com> writes:
[...]
> /** I think this version will print out all letters in the
> upper case format, once it encounters them. But I need only
> the first letter **/
>
> for (i = 0; i < len; i++)
> {
> if (user_name[i] == space)
> continue;
> else
> printf("%c", toupper(user_name[i])
> }
>
> 2 one:
>
> void toupper_initials(char user_name[])
> {
> char space = ' ';
I wouldn't bother with this declaration. I suggest just writing ' '
when you want to refer the space character.
> bool is_space = 0;
If you're using the "bool" defined in <stdbool.h>, I suggest using the
constants false and true rather than 0 and 1.
> int len = strlen(user_name);
> int i;
>
> for (i = 0; i < len; )
> {
> if (is_space)
> {
> while (user_name[i] == space)
> user_name[i++];
> is_space = 0; /** I guess short version 'is_space;' would be suffice because 'is_space;' will mean is_space is 0 as set initially **/
No, `is_space;` by itself does nothing.
>
> printf("%c", toupper(user_name[i]));
Strictly speaking,
toupper(user_name[i])
should be
toupper((unsigned char)username[i]).
In practice, it's probably not worth worrying about at this stage; the
code you have will work fine for all ordinary characters. But put it on
your list of things to think about later. (It's an unfortunate
historical characteristic of the functions defined in <ctype.h>.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-10-26 09:44 -0700 |
| Message-ID | <96a31cae-54ad-418b-830d-51bbc7455618@googlegroups.com> |
| In reply to | #73834 |
Accidentally the unfinished post has been published. Deleted. If it still appears somewhere, please, ignore, and look at the next one. Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-26 11:21 -0700 |
| Message-ID | <lnziz5ebi6.fsf@kst-u.example.com> |
| In reply to | #73870 |
Alla _ <modelling.data@gmail.com> writes:
> Accidentally the unfinished post has been published.
> Deleted. If it still appears somewhere, please, ignore,
> and look at the next one. Thank you!
Deleting articles after you've posted them is pretty much impossible.
Usenet does support cancellation of article by sending a specially
formatted cancel article, but most servers ignore cancellations (due to
past abuse).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-10-26 09:47 -0700 |
| Message-ID | <252c2ddf-4fbd-4703-b173-04beb162cfe3@googlegroups.com> |
| In reply to | #73834 |
Moving forward with the function part of the program.
I am trying to make the program take into account possible
spaces, including spaces at initial places of an array.
I stumbled upon using the outside for loop, inner while loop,
and Boolean expression.
Goal: if user_name[i] is space, then the program should
increment i until non-space character is found, and print out
only this character, skipping all the rest. Then, if another
space is found, it does the same.
If there are no spaces, including no spaces in the beginning
(which should also be considered above), the program prints out
only one initial letter [0].
I have a few versions, but none would work. I even see why,
but it's been two hours (two shameful hours) that I can't
come up with anything decent.
(skipping the name of the function, and initial declarations)
1 one:
/** I think this version will print out all letters in the
upper case format, once it encounters them. But I need only
the first letter **/
for (i = 0; i < len; i++)
{
if (user_name[i] == space)
continue;
else
printf("%c", toupper(user_name[i])
}
2 one:
void toupper_initials(char user_name[])
{
char space = ' ';
bool is_space = 0;
int len = strlen(user_name);
int i;
for (i = 0; i < len; i++)
{
if (is_space)
{
while (user_name[i] == space)
user_name[i++];
is_space = 0;
/** I guess short version 'is_space;' would be suffice because
'is_space;' will mean is_space is 0 as set initially **/
printf("%c", toupper(user_name[i]));
/** this should print an i-th character if it is not a space, but
I don't see that it will print out only one letter, and not
all of them **/
}
else // there are no spaces in the beginning
{
printf("%c", toupper(user_name[0]));
is_space = 1;
}
}
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-10-26 10:47 -0700 |
| Message-ID | <a279cb90-1a87-4595-a855-5e92c5a9fd56@googlegroups.com> |
| In reply to | #73871 |
On Monday, October 26, 2015 at 8:47:22 PM UTC+4, Alla _ wrote:
> Moving forward with the function part of the program.
> I am trying to make the program take into account possible
> spaces, including spaces at initial places of an array.
Problems raised in this topic are not solved yet,
but here is the program that is closer to the final
version (I hope to be able to solve issues that I think
are important to finalize this program - only those
I raised in the previous post). I tested it, and, to my
surprise, it works :)
/**
* Program to print initials of the name
* entered by the user
**/
#include <stdio.h>
#include <string.h>
void toupper_initials(char user_name[]);
int main(void)
{
char name[80];
printf("Please, enter your name (up to 80 characters)\n");
while (!(scanf("%79[A-Z | a-z]", name) && getchar() == '\n'))
{
printf("\a");
printf("The name should contain only letters from Enlgish alphabet\n");
while (getchar() != '\n');
}
toupper_initials(name);
return 0;
}
/** Function to print initials in uppercase letters
without spaces
**/
void toupper_initials(char user_name[])
{
char initials[80];
char space = ' ';
int len = strlen(user_name);
int i;
printf("%c", toupper(user_name[0]));
for (i = 1; i < len; i++)
{
if (user_name[i] == space)
{
initials[i] = toupper(user_name[i + 1]);
printf("%c", initials[i]);
}
}
printf("\n");
}
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-10-26 18:20 +0000 |
| Message-ID | <n0lqpk$spi$1@dont-email.me> |
| In reply to | #73887 |
On 26/10/15 17:47, Alla _ wrote:
> On Monday, October 26, 2015 at 8:47:22 PM UTC+4, Alla _ wrote:
>> Moving forward with the function part of the program.
>> I am trying to make the program take into account possible
>> spaces, including spaces at initial places of an array.
>
>
> Problems raised in this topic are not solved yet,
> but here is the program that is closer to the final
> version (I hope to be able to solve issues that I think
> are important to finalize this program - only those
> I raised in the previous post). I tested it, and, to my
> surprise, it works :)
We'll get there in a minute. First, let's take a look at the compiler's
warnings:
help.c: In function ‘toupper_initials’:
help.c:36:21: warning: conversion to ‘int’ from ‘size_t’ may alter its
value [-Wconversion]
int len = strlen(user_name);
^
Don't worry about this one for now, BUT write on your to-do list (if you
haven't already) 'The size_t type'.
help.c:39:5: warning: implicit declaration of function ‘toupper’
[-Wimplicit-function-declaration]
printf("%c", toupper(user_name[0]));
^
The toupper function is prototyped in <ctype.h> so you need to add that
to your list of includes.
help.c:39:5: warning: nested extern declaration of ‘toupper’
[-Wnested-externs]
#include <ctype.h> will fix that one too.
help.c:45:34: warning: conversion to ‘char’ from ‘int’ may alter its
value [-Wconversion]
initials[i] = toupper(user_name[i + 1]);
^
You don't have to worry about that one. The toupper function takes an
int, whereas user_name[i + 1] is a char, but it'll be fine as long as
user_name[i + 1] is non-negative, which it probably is for your test data.
>
> /**
> * Program to print initials of the name
> * entered by the user
> **/
> #include <stdio.h>
> #include <string.h>
>
> void toupper_initials(char user_name[]);
>
> int main(void)
> {
> char name[80];
>
> printf("Please, enter your name (up to 80 characters)\n");
>
> while (!(scanf("%79[A-Z | a-z]", name) && getchar() == '\n'))
In all honesty... can you truly say you understand that line of code?
(As soon as I saw it, my eyes glazed over. I don't even want to go
there.) Have you not recently demonstrated that you've learned the
fgets() function? Wouldn't it be so much easier to use that?
> {
> printf("\a");
> printf("The name should contain only letters from Enlgish alphabet\n");
> while (getchar() != '\n');
> }
>
> toupper_initials(name);
>
> return 0;
> }
>
> /** Function to print initials in uppercase letters
> without spaces
> **/
>
> void toupper_initials(char user_name[])
> {
> char initials[80];
> char space = ' ';
> int len = strlen(user_name);
> int i;
>
> printf("%c", toupper(user_name[0]));
>
> for (i = 1; i < len; i++)
> {
> if (user_name[i] == space)
> {
> initials[i] = toupper(user_name[i + 1]);
>
> printf("%c", initials[i]);
> }
> }
>
> printf("\n");
> }
Because my news software sometimes conflates contiguous spaces, I've
replaced the space character with the _ underscore character in the
following test inputs and outputs. (Obviously I used spaces when
actually running the program.) The [brackets] are not part of the data.
[Alan_Bell][AB]
[_Alan_Bell][_B]
[__Alan_Bell][_AB]
[___Alan_Bell][__AB]
[Alan__Bell][A_B]
[Alan___Bell][A__B]
[Alan_Bob_Cooper][ABC]
[______Alan______Bob______Cooper][_____A_____B_____C]
[Alan Bell________][AB_______@]
@ was the null character. How did that sneak in?
As you can see, the program works for normal inputs, but doesn't handle
unusual inputs very well. Still some work to do, I think.
If I were you, I would think very, very seriously about using a simple
state machine along the lines I suggested earlier.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-10-26 11:56 -0700 |
| Message-ID | <faa06526-12f3-4014-8bc0-3803e5a16106@googlegroups.com> |
| In reply to | #73895 |
On Monday, October 26, 2015 at 10:20:58 PM UTC+4, Richard Heathfield wrote:
> On 26/10/15 17:47, Alla _ wrote:
> > On Monday, October 26, 2015 at 8:47:22 PM UTC+4, Alla _ wrote:
> >> Moving forward with the function part of the program.
> >> I am trying to make the program take into account possible
> >> spaces, including spaces at initial places of an array.
> >
> >
> > Problems raised in this topic are not solved yet,
> > but here is the program that is closer to the final
> > version (I hope to be able to solve issues that I think
> > are important to finalize this program - only those
> > I raised in the previous post). I tested it, and, to my
> > surprise, it works :)
>
> We'll get there in a minute. First, let's take a look at the compiler's
> warnings:
>
> help.c: In function 'toupper_initials':
> help.c:36:21: warning: conversion to 'int' from 'size_t' may alter its
> value [-Wconversion]
> int len = strlen(user_name);
> ^
> Don't worry about this one for now, BUT write on your to-do list (if you
> haven't already) 'The size_t type'.
>
> help.c:39:5: warning: implicit declaration of function 'toupper'
> [-Wimplicit-function-declaration]
> printf("%c", toupper(user_name[0]));
> ^
>
> The toupper function is prototyped in <ctype.h> so you need to add that
> to your list of includes.
>
>
> help.c:39:5: warning: nested extern declaration of 'toupper'
> [-Wnested-externs]
>
> #include <ctype.h> will fix that one too.
>
> help.c:45:34: warning: conversion to 'char' from 'int' may alter its
> value [-Wconversion]
> initials[i] = toupper(user_name[i + 1]);
> ^
> You don't have to worry about that one. The toupper function takes an
> int, whereas user_name[i + 1] is a char, but it'll be fine as long as
> user_name[i + 1] is non-negative, which it probably is for your test data.
>
> >
> > /**
> > * Program to print initials of the name
> > * entered by the user
> > **/
> > #include <stdio.h>
> > #include <string.h>
> >
> > void toupper_initials(char user_name[]);
> >
> > int main(void)
> > {
> > char name[80];
> >
> > printf("Please, enter your name (up to 80 characters)\n");
> >
> > while (!(scanf("%79[A-Z | a-z]", name) && getchar() == '\n'))
>
> In all honesty... can you truly say you understand that line of code?
> (As soon as I saw it, my eyes glazed over. I don't even want to go
> there.) Have you not recently demonstrated that you've learned the
> fgets() function? Wouldn't it be so much easier to use that?
>
You wouldn't believe it (I am also surprised by that), but
yes, I understand that part; this is why I decided to use it,
because it was easier for me, at this stage, than fgets - I am
not yet confident about fgets and need to practice it. Not that I
have mastered scanf! Far from it; I have only learned few features.
I do not yet understand the term "state machine"; I will
look into it. Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-10-26 19:24 +0000 |
| Message-ID | <87mvv5juuv.fsf@bsb.me.uk> |
| In reply to | #73900 |
Alla _ <modelling.data@gmail.com> writes:
> On Monday, October 26, 2015 at 10:20:58 PM UTC+4, Richard Heathfield wrote:
>> On 26/10/15 17:47, Alla _ wrote:
<snip>
>> > char name[80];
>> >
>> > printf("Please, enter your name (up to 80 characters)\n");
>> >
>> > while (!(scanf("%79[A-Z | a-z]", name) && getchar() == '\n'))
>>
>> In all honesty... can you truly say you understand that line of code?
>> (As soon as I saw it, my eyes glazed over. I don't even want to go
>> there.) Have you not recently demonstrated that you've learned the
>> fgets() function? Wouldn't it be so much easier to use that?
>
> You wouldn't believe it (I am also surprised by that), but
> yes, I understand that part;
I don't think you do. If you did you would not have included | as a
valid character in a name, nor would you have included a space character
twice.
That part aside, let's look at the logic. What happens when the user
types "ben!" as the name? The scanf call returns 1 and getchar()
returns '!' which is not '\n' so the inner condition is false and "not"
of it is true. The program enters the body of the loop and prints an
error message.
Now the program tries again. It can't see any characters that might be
part of a name (there is nothing but a newline to be read) so the scanf
call returns 0 and the getchar function is not called. Again the inner
condition is false and the "not" of it is true. Nothing has changed and
the program enters the body of the loop again. Do you see the problem?
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-10-26 12:01 -0700 |
| Message-ID | <c5cae489-40f7-4a10-8afe-21d9eaf38197@googlegroups.com> |
| In reply to | #73895 |
help.c:39:5: warning: implicit declaration of function 'toupper'
[-Wimplicit-function-declaration]
printf("%c", toupper(user_name[0]));
^
The toupper function is prototyped in <ctype.h> so you need to add that
to your list of includes.
Yes, I have already done that. I will have to read
your posts, and posts I see from Keith below on
warning messages. My compiler didn't produce any
complaint on this 'toupper' issue. But when I tried
to compile this program on my Virtual Machine, the
compiler there did generate an error message.
[toc] | [prev] | [next] | [standalone]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Date | 2015-10-26 17:48 +0000 |
| Message-ID | <n0lotq$kh1$1@dont-email.me> |
| In reply to | #73871 |
On 26/10/15 16:47, Alla _ wrote:
> Moving forward with the function part of the program.
> I am trying to make the program take into account possible
> spaces, including spaces at initial places of an array.
Okay, so you have several possible states:
State 1: you are looking for an initial letter;
State 2: you have found an initial letter, and now you're looking for a
space;
State 3: you've found a space, so you're back in State 1;
State 4: you've run out of data.
We can more or less ignore State 4, since it can be covered by the loop
condition, so that leaves three states.
Let's bear that in mind for later on.
> I stumbled upon using the outside for loop, inner while loop,
> and Boolean expression.
"stumbled upon" is okay for a short time, but there comes a point where
you have to start /designing/ your programs. :-) Now might be a good
time to give some consideration to that idea.
> Goal: if user_name[i] is space, then the program should
> increment i until non-space character is found, and print out
> only this character, skipping all the rest. Then, if another
> space is found, it does the same.
So we're looking at something like:
int searching_for_initial = 1;
int found_initial = 2;
int searching_for_space = 3;
int state = searching_for_initial;
i = 0;
while(data[i] != '\0')
{
if(state == searching_for_initial)
{
if(isalpha(data[i]))
{
state = found_initial;
putchar(toupper(data[i]));
}
}
else if(state == searching_for_space)
{
if(isspace(data[i]))
{
state = searching_for_initial;
}
}
else if(state == found_initial)
{
state = searching_for_space;
}
++i; /* easy to forget this! */
}
> /** I think this version will print out all letters in the
> upper case format, once it encounters them. But I need only
> the first letter **/
>
> for (i = 0; i < len; i++)
> {
> if (user_name[i] == space)
> continue;
> else
> printf("%c", toupper(user_name[i])
This won't even compile. Your parentheses don't match, and you're
missing a semicolon. But if that were fixed, this would indeed print
every non-space letter.
> }
>
> 2 one:
>
> void toupper_initials(char user_name[])
> {
> char space = ' ';
> bool is_space = 0;
> int len = strlen(user_name);
> int i;
>
> for (i = 0; i < len; i++)
> {
> if (is_space)
> {
> while (user_name[i] == space)
> user_name[i++];
>
> is_space = 0;
> /** I guess short version 'is_space;' would be suffice because
> 'is_space;' will mean is_space is 0 as set initially **/
What makes you think so? If you write:
is_space;
then nothing will happen. Well, okay, is_space will be evaluated and the
result will be thrown away, but the value of is_space won't magically be
set to 0.
>
> printf("%c", toupper(user_name[i]));
>
> /** this should print an i-th character if it is not a space, but
> I don't see that it will print out only one letter, and not
> all of them **/
> }
>
> else // there are no spaces in the beginning
> {
> printf("%c", toupper(user_name[0]));
> is_space = 1;
> }
>
> }
I think that's just a bit confused. Let me try to replace all the stuff
you missed out, and see if I can make some sense of it:
void toupper_initials(char user_name[])
{
char space = ' ';
bool is_space = 0;
int len = strlen(user_name);
int i;
for (i = 0; i < len; i++)
Okay, so we're going through the characters one at a time.
{
if (is_space)
{
while (user_name[i] == space)
user_name[i++];
That's not really a very good idea. i is the loop counter for the
for-loop. Whilst it can sometimes be worthwhile to 'hack' this value in
the inner loop, at this stage it can only confuse you.
is_space = 0;
printf("%c", toupper(user_name[i]));
Okay, that's going to print out the first non-space character, if there
is one. What if there isn't? What if all the input is spaces?
}
else // there are no spaces in the beginning
{
printf("%c", toupper(user_name[0]));
You meant i, I think, not 0.
is_space = 1;
The problem here is that you're not taking into account the possibility
that the initial letter might be followed by other letters. The way your
program is written, it will treat those as initial letters too.
Take a closer look at this state machine. (Normally, state machines
aren't written quite like this, but I can't remember whether you've come
across #define yet, and also I've made the states slightly more distinct
than is strictly necessary, in an attempt to keep the three aspects of
the search as separate as possible in your mind.)
int searching_for_initial = 1;
int found_initial = 2;
int searching_for_space = 3;
int state = searching_for_initial;
i = 0;
while(data[i] != '\0')
{
if(state == searching_for_initial)
{
if(isalpha(data[i]))
{
state = found_initial;
putchar(toupper(data[i]));
}
}
else if(state == searching_for_space)
{
if(isspace(data[i]))
{
state = searching_for_initial;
}
}
else if(state == found_initial)
{
state = searching_for_space;
}
++i; /* easy to forget this! */
}
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-10-26 10:43 -0700 |
| Message-ID | <lnfv0xfrts.fsf@kst-u.example.com> |
| In reply to | #73834 |
Alla _ <modelling.data@gmail.com> writes:
[...]
> int main(void)
> {
> char name[] = "Alla";
>
> toupper_initials(name);
>
> return 0;
> }
[...]
One small suggestion: Choose test data that gives you as much
information as possible about any errors. Given "Alla", if your program
prints 'A' you can't easily tell whether that's the first letter or the
last one mapped to upper case; if it prints 'L' you can't be sure
whether it's the second or third letter. "Abcd" could make debugging a
bit easier. (Of course you'll also want to make sure your code works
with multiple occurrences of the same letter.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [standalone]
Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]
Back to top | Article view | comp.lang.c
csiph-web