Thread: Need some help with strings...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    Need some help with strings...

    I need to make a c program that will find The Positions and Frequency of character from a string.


    I found the solution...


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int findChar (const char str[], char c)
    {
        int i, j;
    
        for(i = 0; str[i] != '\0'; i++){
            if(str[i] == c){
                return i;
            }
        }
    
        return -1;
    }
    int findCharFreq(const char str[], int c)
    {
        int i, j=0;
        for(i = 0; str[i] != '\0'; i++){
            if(str[i] == c){
                j++;
            }
        }
        return j;
    }
    int main(void)
    {
        int sCount;
        int pos, freq;
        char ch, str[80];
        printf("Please input some text below: \n");
        for(sCount=0; ((str[sCount] = getchar()) != '\n'); ++sCount);
        str[sCount+1] = '\0';
        printf("Please input your desired character: \n");
        ch = getchar();
        pos = findChar(str, ch);
        freq = findCharFreq(str, ch);
        if(pos == -1){
                    printf("\nWe have not find your desired character in the string.\n");
        }
        else{
                    printf("%c is in the postion of %d in the string.\n", ch, pos+1);
                    printf("And the frequency of %c is %d.\n", ch, freq);
        }
    
        system("PAUSE");
    return 0;
    }




    But i have a problem....

    This program will display only one (the first) position of a character in a string....

    but if a character appears more than once in a string???

    how can i make this program to display all the positions of 1 char that appears more than 1 time in a string???

    Can anyone help please i'd really be grateful..

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    As you might guess, it can be easily done in a loop. You started the search at the first element of the string array str[]. You found your first instance of the char you were searching for.

    Where should the next search begin at?

    Right - the very next char after the one you found.

    Give that a shot. Post back if it's wrapping your head into a knot.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    Bro i don't wanna sound like a lazy slut...

    I'm still a newbie and my english is kinda bad......

    Iv'e trying to find a solution for this for 3 hours already...

    can u just recopy the code i posted with your fix if its not a big deal???...

    I wish i could do it on my own but i can't because i found this solution on google and i can't even understand the half of the code

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't have a "fix", prepared for you. It's a bit of a puzzle, but you can do this by hand, with paper and pencil, surely.

    Do it several times, and you'll start to notice the logical patterns you follow to do it - doesn't take 5 minutes. More like two minutes.

    You need less Googling and more working and practicing with C. No amount of Google will do it for you if you don't work with C, as well.

    Get some "skin" into your homework - I don't mean to sound like a slave driver, but...

    You try, and post it, and I'll help. You do nothing more, and I'll do nothing more. Deal?

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    That's fine i guess since i don't have any other choice anyway.

    But the problem is that as i said we just started learning about C programming we still haven't even started to learn about strings or matrix and yet the professor gave me this seminar work (the program i posted) that i have to finish.... and i don't even know about functions,pointers and etc...and i have to do this program as a seminar work...asap

    it sounds ........ed up doesn't it?

    i have to learn c programming from google to work my way out on this..

    yea i guess i should start working and learning on my own,to be able to finish this program which will take probably a week or more since i don't have so much time...

    But still i appreciate your posts.

    Better a little help than no help.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, time for an update.

    You have most of the code you need. What changes need to be made in the logic to save the location (the index in the array), of each instance of the char you're searching for?

    It could be done in here, with a small amount of changes:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int findChar (const char str[], char c)
    {
        int i, j;
    
        for(i = 0; str[i] != '\0'; i++){
            if(str[i] == c){
                return i;
            }
        }
    
        return -1;
    }
    int findCharFreq(const char str[], int c)
    {
        int i, j=0;
        for(i = 0; str[i] != '\0'; i++){
            if(str[i] == c){
                j++;
            }
        }
        return j;
    }
    int main(void)
    {
        int sCount;
        int pos, freq;
        char ch, str[80];
        printf("Please input some text below: \n");
        for(sCount=0; ((str[sCount] = getchar()) != '\n'); ++sCount);
        str[sCount+1] = '\0';
        printf("Please input your desired character: \n");
        ch = getchar();
        pos = findChar(str, ch);
        freq = findCharFreq(str, ch);
        if(pos == -1){
                    printf("\nWe have not find your desired character in the string.\n");
        }
        else{
                    printf("%c is in the postion of %d in the string.\n", ch, pos+1);
                    printf("And the frequency of %c is %d.\n", ch, freq);
        }
    
        system("PAUSE");
    return 0;
    }
    And yes, you could probably search all over the net and find some code to copy and paste, but that's not learning anything about programming or C. That''s just practicing searching the net, and copy and pasting.


    So what have you attempted or worked out?

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    unfortunately nothing.... still new and learning

    all i found out is that i need to add another for(loop) =(

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What if you had another array in main(), say an int array with a length longer than any string that you would process.

    If you passed that array into findCharFreq(), you could record ALONG with the frequency of the char, the index of the array that way that char.

    Say the string being searched was:
    "Once a jolly swagman"

    Code:
    //char str is the string array being brought into the function and the string it has is "Once a jolly swagman"
    
    for(i=0;i<length;i++)  //length is another parameter brought into the function as a parameter
        if(str[i] == 'l') {     //we're looking for an el
            freq++;            //found one
            array[i]=1;        //now I've recorded the index of the el, into array[]
       }
    }
    This is not a finished block of code, but you get the idea:

    1) you have to bring in the array, it can't be a local array

    2) you need to bring in the length of the string to be searched, if you want to use it. Using it locally, will get you the size of just the pointer to the array, not the whole array size.

    3) array[] needs to be set to 0, first, before you use it, and reset to 0, after every use. Otherwise your data will be trash.

    4) When you want to know where the el's were in main(), you can print them up, by looping through the array[] you set up. Note that you can't run two char's through there - this is for ONE char only, at a time. If you want to check for 'm', then you need to reset the array[] to all zero's, and do that for every char that you are finding the location of.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  3. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM