Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 huge problem :/
Index -> Programming, C -> C Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bonne




PostPosted: Tue Apr 02, 2013 9:42 am   Post subject: 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.

c:
#include <stdio.h>
#include <stdlib.h>
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!
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Apr 02, 2013 10:42 am   Post subject: 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




PostPosted: Tue Apr 02, 2013 10:45 am   Post subject: Re: huge problem :/

bonne @ 4/2/2013, 9:42 am wrote:
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 <stdio.h>
#include <stdlib.h>
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 Razz)
bonne




PostPosted: Tue Apr 02, 2013 11:01 am   Post subject: 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




PostPosted: Tue Apr 02, 2013 3:40 pm   Post subject: Re: RE:huge problem :/

bonne @ Tue Apr 02, 2013 11:01 am wrote:
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




PostPosted: Wed Apr 03, 2013 2:15 pm   Post subject: Re: RE:huge problem :/

nullptr @ Tue Apr 02, 2013 8:40 pm wrote:
bonne @ Tue Apr 02, 2013 11:01 am wrote:
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




PostPosted: Wed Apr 03, 2013 5:20 pm   Post subject: Re: RE:huge problem :/

bonne @ Wed Apr 03, 2013 2:15 pm wrote:
nullptr @ Tue Apr 02, 2013 8:40 pm wrote:
bonne @ Tue Apr 02, 2013 11:01 am wrote:
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




PostPosted: Wed Apr 03, 2013 6:18 pm   Post subject: RE:huge problem :/

Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.
Sponsor
Sponsor
Sponsor
sponsor
bonne




PostPosted: Thu Apr 04, 2013 8:45 am   Post subject: Re: RE:huge problem :/

Insectoid @ Wed Apr 03, 2013 11:18 pm wrote:
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.

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




PostPosted: Thu Apr 04, 2013 8:02 pm   Post subject: Re: RE:huge problem :/

bonne @ 4/4/2013, 8:45 am wrote:
Insectoid @ Wed Apr 03, 2013 11:18 pm wrote:
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.

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 Smile
bl0ckeduser




PostPosted: Fri Apr 05, 2013 8:52 am   Post subject: Re: huge problem :/

Here's a quick attempt at a C solution to your problem.
code:

#include <stdio.h>
#include <stdlib.h>

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);
}


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


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




PostPosted: Fri Apr 05, 2013 9:58 am   Post subject: Re: RE:huge problem :/

Cancer Sol @ Fri Apr 05, 2013 1:02 am wrote:
bonne @ 4/4/2013, 8:45 am wrote:
Insectoid @ Wed Apr 03, 2013 11:18 pm wrote:
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.

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 Smile

This is why I'm here.
wtd




PostPosted: Fri Apr 05, 2013 11:11 am   Post subject: 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:

code:
char *str = "Hello world";
int i;

for (i = 0; str[i] != 0; i++)
{
    printf("%c\n", str[i]);
}


This is pretty straightforward. The zero at the end of the string is added automatically by the compiler. We then loop over the string like any other array. Our condition of termination is just that the character not equal zero.

If we want to print all of the two character sequences, we can do that. We just need to make sure the character after the current one isn't zero

code:
char *str = "Hello world";
int i;

for (i = 0; str[i + 1] != 0; i++)
{
    printf("%c%c\n", str[i], string[i + 1]);
}


Or perhaps we can express this using a loop to print the characters inside each two character section.

code:
char *str = "Hello world";
int i;

for (i = 0; str[i + 1] != 0; i++)
{
    int j;

    for (j = 0; j < 2; j++)
    {
        printf("%c", string[i + j]);
    }

    printf("\n");
}


Now that we've done this, we can easily modify it for three characters.

code:
char *str = "Hello world";
int i;

for (i = 0; str[i + 2] != 0; i++)
{
    int j;

    for (j = 0; j < 3; j++)
    {
        printf("%c", string[i + j]);
    }

    printf("\n");
}


Or, more generally...

code:
void print_substrings(char *str, int length)
{
    int i;

    for (i = 0; str[i + length - 1] != 0; i++)
    {
        int j;

        for (j = 0; j < length; j++)
        {
            printf("%c", string[i + j]);
        }

        printf("\n");
    }
}


Now, another problem we have to determine is if two strings are the same. To do this we have to loop over both and compare them one character at a time. We can cheat a bit by returning zero (false) from the function the first time we find a mismatch. If we make it to the end of the loop, we can assume the two are equal. [i]But
, 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;
    }
}


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;
}


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);
    }
}


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!


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;
        }
    }
}


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;
}


Hopefully you now have a better idea of how string handling works in C, and how your problem can be solved.
Cancer Sol




PostPosted: Fri Apr 05, 2013 5:35 pm   Post subject: Re: RE:huge problem :/

bonne @ 4/5/2013, 9:58 am wrote:
Cancer Sol @ Fri Apr 05, 2013 1:02 am wrote:
bonne @ 4/4/2013, 8:45 am wrote:
Insectoid @ Wed Apr 03, 2013 11:18 pm wrote:
Those are pretty nice string functions. Unfortunately, C strings don't do all that much. These functions might be more useful.

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 Smile

This is why I'm here.

http://lmgtfy.com/?q=+How+to+use+substring+in+C
Try that.
Insectoid




PostPosted: Fri Apr 05, 2013 6:44 pm   Post subject: RE:huge problem :/

Not this time, Cancer Sol.

Posted Image, might have been reduced in size. Click Image to view fullscreen.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: