• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NUCLEAR RABBIT

String help in C

12 posts in this topic

Hello, I am trying to write a program that counts how many times a letter occurs in a string. I know there's easy ways to do it, but I want to use the strchr function to have a search key. I wrote this and for some reason, nothing even happens when I run it! No errors or nothing so I'm not sure what the problem could be! Any help is appreciated sad.png

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{
    const char * random_text = "Once there was this really old man who tranformed into dust. The end!";
    
    int counter = 0;
    int searchKey = 'd';
    char * strPtr = strchr(random_text, searchKey);
    
    do {
        if(strPtr != NULL)
           counter++;
        
        strPtr = strchr(random_text+1, searchKey);
    } while (strPtr != NULL);
    
    printf("%s\n%s\n\n%s%c%s%d%s", "The text:", random_text, "The occurence of ",
           searchKey, " happened ", counter, " times.\n");
    
    return 0;
}
Edited by NUCLEAR RABBIT
0

Share this post


Link to post
Share on other sites

Look more carefully at your loop. You are using strPtr to store the result, but using random_text to search on.  You probably meant to assign the string to the pointer before the loop, and use strPtr as the parameter.

 

ahhhhhhh, thank you!biggrin.png

0

Share this post


Link to post
Share on other sites

 

Hello, I am trying to write a program that counts how many times a letter occurs in a string. I know there's easy ways to do it, but I want to use the strchr function to have a search key. I wrote this and for some reason, nothing even happens when I run it! No errors or nothing so I'm not sure what the problem could be! Any help is appreciated sad.png

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{
    const char * random_text = "Once there was this really old man who tranformed into dust. The end!";
    
    int counter = 0;
    int searchKey = 'd';
    char * strPtr = strchr(random_text, searchKey);
    
    do {
        if(strPtr != NULL)
           counter++;
        
        strPtr = strchr(random_text+1, searchKey);
    } while (strPtr != NULL);
    
    printf("%s\n%s\n\n%s%c%s%d%s", "The text:", random_text, "The occurence of ",
           searchKey, " happened ", counter, " times.\n");
    
    return 0;
}

 

I did it like this:

#include <stdio.h>
#include <string.h>

int main( int argc, const char * argv[] )
{
    char random_text[] = "Once there was this really old man who tranformed into dust. The end!";
    int counter = 0;
    int searchKey = 'd';
    char * strPtr = NULL;

    strPtr = strchr( random_text, searchKey );
    while ( strPtr != NULL )
    {
        counter++;
        strPtr = strchr( strPtr + 1, searchKey );
    }

    printf( "%s\n%s\n\n%s%c%s%d%s",
            "The text:", random_text,
            "The occurence of ", searchKey,
            " happened ", counter, " times.\n" );

    return 0;
}

Be careful when using string literals in C.

0

Share this post


Link to post
Share on other sites
std::string randomString = "Once there was this really old man who tranformed into dust. The end!";
size_t frequency[255];
ZeroMemory(frequency, 255 * sizeof(size_t))
for (size_t counter = 0; counter < randomString.size(); ++counter)
{
    ++frequency[randomString[counter]];
}
 
auto numberOfDs = frequency['d'];
//Same linear runtime but you have all of the frequencies of chars for the string, of course this only works for ASCII strings
Edited by NightCreature83
1

Share this post


Link to post
Share on other sites
std::string randomString = "Once there was this really old man who tranformed into dust. The end!";
size_t frequency[255];
ZeroMemory(frequency, 255 * sizeof(size_t))
for (size_t counter = 0; counter < randomString.size(); ++counter)
{
    ++frequency[randomString[counter]];
}
 
auto numberOfDs = frequency['d'];
//Same linear runtime but you have all of the frequencies of chars for the string, of course this only works for ASCII strings

 

Really nice, provided that C++11 is an option for OP - I recall reading another topic he started where he made a question about a program in C as well.

 

Also, reading @Álvaro's elegant C code makes me realize just how rusty my own C is, and how glad I am to have put away my C hat...

0

Share this post


Link to post
Share on other sites
std::string randomString = "Once there was this really old man who tranformed into dust. The end!";
size_t frequency[255];
ZeroMemory(frequency, 255 * sizeof(size_t))
for (size_t counter = 0; counter < randomString.size(); ++counter)
{
    ++frequency[randomString[counter]];
}
 
auto numberOfDs = frequency['d'];
//Same linear runtime but you have all of the frequencies of chars for the string, of course this only works for ASCII strings

 

The OP is using C, not C++. The OP said he wants to implement this using strchr. Your code invokes undefined behavior if one supplies a string with char values below 0 or above 254 (both of which can easily happen, even in platforms where BIT_CHAR is 8).

1

Share this post


Link to post
Share on other sites

So here is a fixed up C version:

const char* randomString = "Once there was this really old man who tranformed into dust. The end!";
size_t frequency[256];
ZeroMemory(frequency, 256 * sizeof(size_t));
for (size_t counter = 0; counter < strlen(randomString); ++counter)
{
    ++frequency[(unsigned char)randomString[counter]];
}
 
size_t numberOfDs = frequency[(unsigned char)'d'];
//Same linear runtime but you have all of the frequencies of chars for the string, of course this only works for ASCII strings

All fixes to this code are trivial to transform from C++11 to C. You were right in the fact that i missed out one element in the frequency array and that you can have negative numbers in char, so the cast to unsigned char will fix this. And if we are going to exclude the use of ZeroMemory we can replace that with a memset which has a set value of 0.

 

In all fairness all I wanted to do is show a more generic solution to what was asked, which has a O(1) lookup at the end for all characters of a string.

 

Ow and if we are really honest here we are answering what is most likely a school assignment.

Edited by NightCreature83
-2

Share this post


Link to post
Share on other sites


In all fairness all I wanted to do is show a more generic solution to what was asked, which has a O(1) lookup at the end for all characters of a string.

 

Yes, it's a good idea and I have used that kind of code in the past. If you had just described the idea, that would have been fine. But if you are going to post code, make sure its quality is good enough.

 

 


Ow and if we are really honest here we are answering what is most likely a school assignment.

 

Possibly, although I am not sure I care. At least the OP showed some effort. If he now understands what was wrong with his code and he learned a couple of other things from reading our solutions, I don't see anything wrong with the situation.

0

Share this post


Link to post
Share on other sites


    for (size_t counter = 0; counter < strlen(randomString); ++counter);

 

Oh, now this might take time that grows quadratically with the length of the string, depending on how smart the compiler is.

0

Share this post


Link to post
Share on other sites

 


    for (size_t counter = 0; counter < strlen(randomString); ++counter);

 

Oh, now this might take time that grows quadratically with the length of the string, depending on how smart the compiler is.

 

in release that should compile out, compilers are pretty good at seeing what is changing in a loop and what is not.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0