
-----------------------------------
bonne
Tue Apr 02, 2013 9:42 am

huge problem :/
-----------------------------------
Hi guys, I need help with a code that I'm trying do (I'm not a programmer).
I need to open a txt file and read two characters (without quotes) example: "/ /" - "/ **" and "* /" and show to user the amount of each charactere.
I have this code and i can tell to user the amount of @ in a txt file.

#include 
#include 
int main ()
{
    FILE * pFile;
    char * strstr ( const char *, const char * );
    int c;
    int a= 0;
    pFile = fopen ("outro.txt", "r");
        if (pFile !=NULL)
        {
            do { //DO
            c = fgetc(pFile);
            if (c=='@')a++;
            }//DO
            while (c != EOF);
            fclose(pFile);
            printf("The amount of @ = %d \n",a);
        }
    system("pause");
    return 0;
}

But i need do this with these characters:  "/ /" - "/ **" and "* /" 

Please, help!

-----------------------------------
DemonWasp
Tue Apr 02, 2013 10:42 am

RE:huge problem :/
-----------------------------------
What have you tried to do to convert the program you listed there into the program you want?

What problems have you encountered?

Please wrap your code in [ code ] tags next time.

-----------------------------------
Cancer Sol
Tue Apr 02, 2013 10:45 am

Re: huge problem :/
-----------------------------------
Hi guys, I need help with a code that I'm trying do (I'm not a programmer).
I need to open a txt file and read two characters (without quotes) example: "/ /" - "/ **" and "* /" and show to user the amount of each charactere.
I have this code and i can tell to user the amount of @ in a txt file.

#include 
#include 
int main ()
{
    FILE * pFile;
    char * strstr ( const char *, const char * );
    int c;
    int a= 0;
    pFile = fopen ("outro.txt", "r");
        if (pFile !=NULL)
        {
            do { //DO
            c = fgetc(pFile);
            if (c=='@')a++;
            }//DO
            while (c != EOF);
            fclose(pFile);
            printf("The amount of @ = %d \n",a);
        }
    system("pause");
    return 0;
}

But i need do this with these characters:  "/ /" - "/ **" and "* /" 

Please, help!
Umm it's kinda hard understanding what you're trying to say. // and /* */ are comments; the compiler skips it (I think) so the linker won't make it part of the program (it's probably hard for you to understand me since I don't even know what I'm saying sometimes :P)

-----------------------------------
bonne
Tue Apr 02, 2013 11:01 am

RE:huge problem :/
-----------------------------------
I need to access a txt file (outro.txt) and must recognize these characters in this file: / /, / ** and * / that will be with other characters and I need it to return the amount of each of these characters (/ /, / ** and * /) in the file outro.txt.

Example outro.txt:
/ / A b c d e f / g * h i / / j k l m 8? 5 && * /

-----------------------------------
nullptr
Tue Apr 02, 2013 3:40 pm

Re: RE:huge problem :/
-----------------------------------
I need to access a txt file (outro.txt) and must recognize these characters in this file: / /, / ** and * / that will be with other characters and I need it to return the amount of each of these characters (/ /, / ** and * /) in the file outro.txt.

Example outro.txt:
/ / A b c d e f / g * h i / / j k l m 8? 5 && * /

What should the program return with this input?

-----------------------------------
bonne
Wed Apr 03, 2013 2:15 pm

Re: RE:huge problem :/
-----------------------------------
I need to access a txt file (outro.txt) and must recognize these characters in this file: / /, / ** and * / that will be with other characters and I need it to return the amount of each of these characters (/ /, / ** and * /) in the file outro.txt.

Example outro.txt:
// A b c d e f / g * h i // j k l m 8? 5 && * /

What should the program return with this input?

I have this entries in "outro.txt":
// A b c d e f /** h i // j k l m 8? 5 && * /
//
/**   asdfggh // asasd

I want the program show me the totals for: "//" , "/**" and "*/" in "outro.txt"
I know i need to use substring but i don't know how to use...

-----------------------------------
nullptr
Wed Apr 03, 2013 5:20 pm

Re: RE:huge problem :/
-----------------------------------
I need to access a txt file (outro.txt) and must recognize these characters in this file: / /, / ** and * / that will be with other characters and I need it to return the amount of each of these characters (/ /, / ** and * /) in the file outro.txt.

Example outro.txt:
// A b c d e f / g * h i // j k l m 8? 5 && * /

What should the program return with this input?

I have this entries in "outro.txt":
// A b c d e f /** h i // j k l m 8? 5 && * /
//
/**   asdfggh // asasd

I want the program show me the totals for: "//" , "/**" and "*/" in "outro.txt"
I know i need to use substring but i don't know how to use...

Suppose you complete your program and run it with this "outro.txt". What should the program output?

You can do it using substring, but there are better ways available. Take a look at this document for some String functions that could be useful: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

-----------------------------------
Insectoid
Wed Apr 03, 2013 6:18 pm

RE:huge problem :/
-----------------------------------
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. [url=http://www.cplusplus.com/reference/cstring/]These functions might be more useful.

-----------------------------------
bonne
Thu Apr 04, 2013 8:45 am

Re: RE:huge problem :/
-----------------------------------
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. 
Hi Insectoid  and nullptr, thanks a lot for your help but I so noob that i don't know how to use substring in my code.
I know just basic things in C...
Can some one teach me how to use substring in my code or make some example for "how to use" with external txt file, please?

-----------------------------------
Cancer Sol
Thu Apr 04, 2013 8:02 pm

Re: RE:huge problem :/
-----------------------------------
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. 
Hi Insectoid  and nullptr, thanks a lot for your help but I so noob that i don't know how to use substring in my code.
I know just basic things in C...
Can some one teach me how to use substring in my code or make some example for "how to use" with external txt file, please?
Try using google. If you can't find any, then ask :)

-----------------------------------
bl0ckeduser
Fri Apr 05, 2013 8:52 am

Re: huge problem :/
-----------------------------------
Here's a quick attempt at a C solution to your problem.
[code]
#include 
#include 

typedef struct {
	struct trie_t* map[256];
	int key;
} trie_t;

trie_t* mktrie()
{
	trie_t* t = malloc(sizeof(trie_t));
	int i;
	if (!t) {
		fprintf(stderr, "couldn't allocate trie\n");
		exit(1);
	}
	for (i = 0; i < 256; ++i)
		t->map[i] = NULL;
	t->key = 0;
	return t;
}

void mkentry(trie_t *n, char* s, int key)
{
	trie_t *c = n;
	for (; *s; ++s) {
		if (c->map[*s] == NULL)
			c->map[*s] = mktrie();
		c = c->map[*s];
	}
	c->key = key;
}


int main()
{
	FILE *f;
	int i;
	int c;
	unsigned int counts[3] = {0};
	trie_t *root = mktrie();
	trie_t *curr = root;

	mkentry(root, "//", 1);
	mkentry(root, "/**", 2);
	mkentry(root, "*/", 3);

	if (!(f = fopen("outro.txt", "r"))) {
		fprintf(stderr, "couldn't open file\n");
		exit(1);
	}

	while ((c = fgetc(f)) >= 0) {
		if (curr->map[c] == NULL)
			curr = root;
		if (curr->map[c] != NULL) {
			curr = curr->map[c];
			if (curr->key) {
				++counts[curr->key - 1];
				curr = root;
			}
		}
	}

	for (i = 0; i < 3; ++i)
		printf("%d ", counts[i]);
	printf("\n");

	fclose(f);
}
[/code]

Quick test:
[code]
$ cat outro.txt 
// A b c d e f /** h i // j k l m 8? 5 && * /
//
/** asdfggh // asasd 

$ ./a.out 
4 2 0 
[/code]

tries might be a bit overkill, i don't know. maybe you can use regexes or something.

EDIT: fixed the code to deal with cases like "/////*****//"

-----------------------------------
bonne
Fri Apr 05, 2013 9:58 am

Re: RE:huge problem :/
-----------------------------------
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. 
Hi Insectoid  and nullptr, thanks a lot for your help but I so noob that i don't know how to use substring in my code.
I know just basic things in C...
Can some one teach me how to use substring in my code or make some example for "how to use" with external txt file, please?
Try using google. If you can't find any, then ask :)
This is why I'm here.

-----------------------------------
wtd
Fri Apr 05, 2013 11:11 am

Re: huge problem :/
-----------------------------------
Chars in C are simply 8-bit integers.  As such, they can represent numbers between 0 and 255.  These correspond to various visual characters in the ASCII character set.

A pointer in C is either a 32 or 64-bit integer, depending on your system.  For these purposes, the distinction is unimportant.  A pointer stores the address in memory of some other piece of data.

An array in C is several pieces of data of one type laid out sequentially in memory.  It is represented in code as a pointer to the first element in the array.

A string in C is simply an array of chars.  By convention, strings in C are char arrays which end in the value zero.  The presence of this zero tells string-handling functions that the string has ended.

Let's consider looping over an array of characters:

 != 0; i++)
{
    printf("%c\n", str, the loop might end because one string ended, but the other didn't.  We'll test for this after the loop, since the variable i will still exist.

[code]int strings_equal(char *str1, char *str2)
{
    int i;

    for (i = 0; str1[i] != 0 || str2[i] != 0; i++)
    {
        if (str1[i] != str2[i])
        {
            return 0;
        }
    }

    if (str1[i] == 0 && str2[i] == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}[/code]

It would help to know the length of a string.  We'll just loop until we find the end, and keep track of how many times we loop.  Our loop doesn't actually have to do anything.

[code]int string_length(char *str)
{
    int i;

    for (i = 0; str[i] != 0; i++)
    {
    }

    return i;
}[/code]

Let's revisit printing the substrings, but instead of just printing the characters, we'll build a new string representing the substring.  I'll have to allocate enough memory to hold the substring using malloc.  And then to have space for the zero, we'll add 1.

[code]void print_substrings(char *str, int length)
{
    int i;
    int number_of_chars = length + 1;
    char *substring = malloc(sizeof(char) * number_of_chars);;

    for (i = 0; str[i + length - 1] != 0; i++)
    {
        int j;

        for (j = 0; j < length; j++)
        {
            substring[j] = str[i + j];
        }

        substring[j] = 0; 

        printf("%s\n", substring);
    }
}[/code]

Now, using our ability to find equal strings, what if we want to avoid printing parts of a substring that matches a given string?  So, if we have "Hello world!" and want to print three character substrings, but get the following.  The substring to match would be "ell".

[code]Hel
ell
o w
 wo
wor
orl
rld
ld![/code]

If the current substring matches the one we're looking for, we'll just skip the variable i ahead the length of the substring, but minus one, because the loop will do that part for us.

[code]int strings_equal(char *str1, char *str2)
{
    int i;

    for (i = 0; str1[i] != 0 || str2[i] != 0; i++)
    {
        if (str1[i] != str2[i])
        {
            return 0;
        }
    }

    if (str1[i] == 0 && str2[i] == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void print_substrings_match(char *str, int length, char *match)
{
    int i;
    int number_of_chars = length + 1;
    char *substring = malloc(sizeof(char) * number_of_chars);;

    for (i = 0; str[i + length - 1] != 0; i++)
    {
        int j;

        for (j = 0; j < length; j++)
        {
            substring[j] = str[i + j];
        }

        substring[j] = 0; 

        printf("%s\n", substring);

        if (strings_equal(substring, match))
        {
            i += length - 1;
        }
    }
}[/code]

Now that we have that condition, how hard would it be to take out the printing and just count the number of matches?

[code]int strings_equal(char *str1, char *str2)
{
    int i;

    for (i = 0; str1[i] != 0 || str2[i] != 0; i++)
    {
        if (str1[i] != str2[i])
        {
            return 0;
        }
    }

    if (str1[i] == 0 && str2[i] == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int count_substring_matches(char *str, int length, char *match)
{
    int i;
    int number_of_matches = 0;
    int number_of_chars = length + 1;
    char *substring = malloc(sizeof(char) * number_of_chars);;

    for (i = 0; str[i + length - 1] != 0; i++)
    {
        int j;

        for (j = 0; j < length; j++)
        {
            substring[j] = str[i + j];
        }

        substring[j] = 0; 

        if (strings_equal(substring, match))
        {
            number_of_matches++;
            i += length - 1;
        }
    }

    return number_of_matches;
}[/code]

Hopefully you now have a better idea of how string handling works in C, and how your problem can be solved.

-----------------------------------
Cancer Sol
Fri Apr 05, 2013 5:35 pm

Re: RE:huge problem :/
-----------------------------------
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. 
Hi Insectoid  and nullptr, thanks a lot for your help but I so noob that i don't know how to use substring in my code.
I know just basic things in C...
Can some one teach me how to use substring in my code or make some example for "how to use" with external txt file, please?
Try using google. If you can't find any, then ask :)
This is why I'm here.
http://lmgtfy.com/?q=+How+to+use+substring+in+C
Try that.

-----------------------------------
Insectoid
Fri Apr 05, 2013 6:44 pm

RE:huge problem :/
-----------------------------------
Not this time, Cancer Sol.

http://i.imgur.com/b12RnDO.png

-----------------------------------
Cancer Sol
Sun Apr 07, 2013 6:05 pm

Re: huge problem :/
-----------------------------------
But mine works.
http://i46.tinypic.com/282drww.jpg

Edit: Maybe try that stackoverflow link? Not sure what content it has though.

-----------------------------------
wtd
Sun Apr 07, 2013 11:37 pm

Re: huge problem :/
-----------------------------------
Fewer huge images to prove how well-endowed you are how good you are at searching Google, and more helping.
