|
In the below code, I am attempting to count the number of letters and number of words in a phrase. In researching the return values of isalpha(), I noted that when a letter is present the first character isalpha() output is 1. But the second character isalpha() output is 2? The phrase that was input into this example was the following: How do you organize? Can someone help me with why I am not getting uniform output for the "H" character and the "o" character?
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
float avg = 0;
int spaces = 0;
int letters = 0;
int test = 0;
int ltrcnt = 0;
int spccnt = 0;
while((ch = getchar()) != '\n')
{
test = isalpha(ch);
if(isalpha(ch) == 2)
{
ltrcnt++;
}
if(isspace(ch) == 8)
{
spccnt++;
}
}
printf("The Number Of Words Are %d , The Number Of Letters Are %d ",(spccnt+1),ltrcnt);
}
|
|
|
|
|
isalpha returns zero if the given character is not an alphabet.
Returns non-zero if it is alphabet.
And may return '1' if the alphabet is of upper case, and '2' if it is lower case.
|
|
|
|
|
Yes, note the following defines from ctype.h
#define _UPPER 0x1 /* upper case letter */
#define _LOWER 0x2 /* lower case letter */
#define _DIGIT 0x4 /* digit[0-9] */
#define _SPACE 0x8 /* tab, carriage return, newline, */
#define _PUNCT 0x10 /* punctuation character */
#define _CONTROL 0x20 /* control character */
#define _BLANK 0x40 /* space char */
#define _HEX 0x80 /* hexadecimal digit */
#define _LEADBYTE 0x8000 /* multibyte leadbyte */
#define _ALPHA (0x0100|_UPPER|_LOWER) /* alphabetic character */
|
|
|
|
|
Andrew,
Ahh,
Thank you for directing me to the defines. Since I am an newbie, I have never looked in the header file. Thank you for the answer as well.
|
|
|
|
|
And you don't have to look there, what matters is the documentation: isalpha return a boolean result, either zero or non-zero; since the different non-zero values are not documented, you should not rely on them. Nor on any observation you may make, not in a .h file and not when running a test app.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Cool_Dev,
Thank you for the answer.
|
|
|
|
|
You should familiarise yourself with the MSDN pages[^]; they contain a wealth of useful information.
I must get a clever new signature for 2011.
|
|
|
|
|
Here is a sample code which works just fine:
#include <stdio.h>
#include <windows.h>
#define malloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define free(m) HeapFree(GetProcessHeap(), 0, m)
#pragma warning(disable: 4996)
wchar_t *test()
{
wchar_t aaa[] = L"aaaa,bbbb,cccc,dddd";
static wchar_t *lol = (wchar_t *)malloc(sizeof(wchar_t *) * wcslen(aaa) + 1);
wcscpy(lol, aaa);
return lol;
}
int main(int argc, char **argv)
{
wchar_t *t;
int i;
wchar_t *stuff = test();
t = wcstok(stuff, L",");
for(i = 0; t; t = wcstok(NULL, L","), i++)
{
wprintf(L"::%s\n", t);
}
return 0
}
This prints
::aaaa
::bbbb
::cccc
::dddd
Now, here is sample code which just dont work and i got no idea why. Its a wcstok inside of wcstok loop.
#include <windows.h>
#include <stdio.h>
#define malloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define free(m) HeapFree(GetProcessHeap(), 0, m)
#pragma warning(disable: 4996)
wchar_t *test()
{
wchar_t aaa[] = L"aaaa,bbbb,cccc,dddd";
static wchar_t *lol = (wchar_t *)malloc(sizeof(wchar_t *) * wcslen(aaa) + 1);
wcscpy(lol, aaa);
return lol;
}
wchar_t *lolz(wchar_t *lol)
{
int i = 2;
static wchar_t *stuff = (wchar_t *)malloc(sizeof(wchar_t *) * (wcslen(lol) + MAX_PATH)*i);
wcscpy(stuff, L" ");
while(i > 0)
{
wcscat(stuff, lol);
wcscat(stuff, L",");
i--;
}
return stuff;
}
int main(int argc, char **argv)
{
wchar_t *t;
int i;
wchar_t *stuff = test();
t = wcstok(stuff, L",");
for(i = 0; t; t = wcstok(NULL, L","), i++)
{
wprintf(L"::%s\n", t);
wchar_t *get = lolz(t);
wchar_t *t2;
int i2;
t2 = wcstok(get, L",");
for(i2 = 0; t2; t2 = wcstok(NULL, L","), i2++)
{
wprintf(L":%s\n", t2);
}
wprintf(L"End for: %s [%d]\n\n", t, i);
}
return 0;
}
this should print:
::aaaa
:aaaa
:aaaa
end for aaaa
::bbbb
:bbbb
:bbbb
end for bbbb
You can see from the code, like, for each main tokens it should get subtokens, but it prints only:
::aaaa
:aaaa
:aaaa
end for aaaa
and thats it. Why? thanks
011011010110000101100011011010000110100101101110
0110010101110011
modified on Thursday, January 13, 2011 10:19 AM
|
|
|
|
|
Did you try to use your debugger to see what's happening ?
Also, please use the code tags when pasting code, because it is very difficult to read right now.
|
|
|
|
|
Have wrapped the code with tags but it is all red now...
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Cedric Moonen wrote: please use the code tags
now that is confusing. What we want is HTML <PRE> tags, which you can type yourself or get by clicking the "code block" widget; we do not want <CODE> tags for a multi-line snippet!
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
i have used exactly pre trags, not the code tags.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
I was only pointing out the wording of the advice was a bit confusing.
I have no doubt you did use PRE tags, you got the right font and background, I just don't know how you would get the red foreground color. You didn't have both PRE and CODE tags, did you?
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Nope. Just pre tags. That background is indeed strange thing. Maybe something with parsing is wrong, or i dont know...
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
did you escape the < signs properly, i.e. use < (which you get for free when pasting code while "Encode < characters when pasting" is checked)?
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Heh, that was it. Now code is properly highlighted. My bad :P
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
And that made me update my little tip article here[^].
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Yes right, I should have said "code block", I was typing too fast
|
|
|
|
|
Mixing wcstok calls for different strings does not work. See the Note here[^]. You need to use wcstok_s to work round this limitation.
Note: Please use <pre></pre> tags round your code rather than <code></code> - the "code block" button does it for you.
I must get a clever new signature for 2011.
|
|
|
|
|
pre tags for some reason made code red. But.
Thanks for the info and link, it helped.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
csrss wrote: pre tags for some reason made code red.
Strange, I've not seen that before; drunken hamsters maybe.
I must get a clever new signature for 2011.
|
|
|
|
|
Ok, replying to my own thread, like Richard MacCutchan pointed out, in such case wcstok cannot be used. Here is working example.
#include <windows.h>
#include <stdio.h>
#define malloc(s) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, s)
#define free(m) HeapFree(GetProcessHeap(), 0, m)
#pragma warning(disable: 4996)
wchar_t *test()
{
wchar_t aaa[] = L"aaaa,bbbb,cccc,dddd";
static wchar_t *lol = (wchar_t *)malloc(sizeof(wchar_t *) * wcslen(aaa) + 1);
wcscpy(lol, aaa);
return lol;
}
wchar_t *lolz(wchar_t *lol)
{
int i = 2;
static wchar_t *stuff = (wchar_t *)malloc(sizeof(wchar_t *) * (wcslen(lol) + MAX_PATH)*i);
wcscpy(stuff, L" ");
while(i > 0)
{
wcscat(stuff, lol);
wcscat(stuff, L",");
i--;
}
return stuff;
}
int main(int argc, char **argv)
{
wchar_t *t, *nexttok;
int i;
wchar_t *stuff = test();
t = wcstok_s(stuff, L",", &nexttok);
for(i = 0; t; t = wcstok_s(NULL, L",", &nexttok), i++)
{
wprintf(L"::%s\n", t);
wchar_t *get = lolz(t);
wchar_t *t2, *nexttok2;
int i2;
t2 = wcstok_s(get, L",", &nexttok2);
for(i2 = 0; t2; t2 = wcstok_s(NULL, L",", &nexttok2), i2++)
{
wprintf(L":%s\n", t2);
}
wprintf(L"End for: %s [%d]\n\n", t, i);
}
return 0;
}
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Hi all.
I am reading some value from registry within a COM module.
The code runs fine in Windows XP and in Windows 7 locally but
in remote I am not able to read the value from the XP.
I am using CRegistry class to access the registry.
|
|
|
|
|
Debojyoti Majumder wrote: in remote I am not able to read the value from the XP.
Make sure that the Remote XP machine doesn't disabled the Remote Registry service
locally via the Windows Services MMC.
|
|
|
|
|
Hi All,
I have created 32 threads , out of these 20 threads are having infinite loop using while (1) {}. Remaining threads are also having infinite loop using while (1) {} but with some print statement. I have observed (With thirdparty utility) that threads WITHOUT print statement takes more time to context switch. Its almost few mins for me to get nothing on console because threads without printf were executed. After some time threads with printf statement executed in a shorter duration as my main () exited after a ::Sleep (300000). Why those threads without printf statement take much time to execute. Can any one have any idea...
Thanks
|
|
|
|
|