techies,
i don't where to start to countr two characters in any patterns or from any words like "ei" from "Receive eieiten pooeesiec".
can you give some hints?
Printable View
techies,
i don't where to start to countr two characters in any patterns or from any words like "ei" from "Receive eieiten pooeesiec".
can you give some hints?
strstr
> strstr
strstr() in a loop.
i am thinking to use not strstr. suppose, i don't know about str. i just want to use one character at a time. how to remember previous character and current char?
Linked Lists!, with a 'next char node' and/or a 'previous char node'... :)
Or a loop, with something like;
Code:char * s = "Hello";
while(*s)
{
// note: you should be checking if p is going to be valid, but thats upto you.
// on the first iteration its not going to work... a simple 'if' statement or for() loop would fix this.
char p = *s-1, c = *s; // p is prev, c is current char
*s++;
}
If the OP doesn't know about strings, I highly doubt they know about pointers. :)
Perhaps something like this:
Code:char s[] = "Receive eieiten pooeesiec";
size_t x, len = strlen(s);
for(x = 1; x < len; x ++) {
s[x]; /* char */
s[x-1]; /* previous char */
}
Yes, well, that's a risk I'll have to take. :) The more [solutions] the merrier.
If you're just looking for either 'e' or 'i' followed by either an 'e' or an 'i', then I suggest using something like this:
You'll notice I provided links for the functions I used. Say, maybe I should get codeform to do that . . . [edit] http://cboard.cprogramming.com/showp...6&postcount=20 [/edit]Code:char str[] = "Receive eieiten pooeesiec";
char look[] = "ei";
char *p;
p = strpbrk(str, look);
if(strchr(look, p[1])) {
/* found, p[0] and p[1] */
}
Good idea :)
BUT, what if I haven't studied pointer, array and functions too. I've just finished studying loop and basic of C like getting characters or lines from key board, and counting them.
Isn't there any way with much knowledge?
You've managed to get three people to do your homework for you, or very close to it. Let's see what you actually have done.
Quzah.
sorry, u r interpreting wrongly. I am not a computer student nor it's a homework. I am just following book and learning C from that. And, what I am asking is WHAT I don't know, or don't know how to start?
I've done word counting program, vowel counting. But it's out of my reach, i think. I don't know how to do two character comparision at a time. Hope cleared!
Use one variable to track the current character you've just read, and one to hold the one you read right before that. If they're not the same, move this one into the "last time" holder, and read a new one into the "just read" holder. Repeat until done.
Quzah.
plz help me. it is stucking in second getchar().
Code:while((ch=getchar())!='#')
{
ch1 = ch;
ch2=getchar();
if(ch1=='a' && ch2=='b')
count++;
}
printf("pattern ab = %d\n",count);
Something like that?Code:while( (ch=getchar()) != '# ) /* read until you find a # */
{
if( ch == first_char_to_find )
{
int ch2 = getchar( );
if( ch2 == second_char_to_find )
yay++;
else
ungetc( ch2, stdin );
}
}
Quzah.
thank you Quzah. but, can you explain what stdin and ungetc? Upto this chapter, i haven't seen ungetc. I have just seen getc, getcha.
And, suppose, the user enter following line in the prompt:
am testing eieseisie michael eissen, the footballer#
Does the line upto # will be in buffer and read one by one by first getchar() and pass to char ch ?
stdin is the standard input stream. It's commonly the input from your keyboard.
stdout is the standard output stream, commonly you see this output on your monitor.
You can scatter a few printf statements around in the loop to get a better understanding of it. All it does is read one character at a time over and over from the input stream, then compares it.
ungetc will let you replace the last character you've read back onto the input stream, provided you've kept track of it. You must unget the same character you just pulled off of it, and you can't call it multiple times to unget more than one character. It only does one.
Quzah.