Thread: How do i return values in a char string?

  1. #1
    Registered User Kupo's Avatar
    Join Date
    Dec 2001
    Posts
    36

    How do i return values in a char string?

    i want to be able to return.. say.. how many letter L's there are in a char string, any help or pointers would be great , as i'm new to C++, and am getting far too ambitious a newbie

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    int countChars(const char* str, char specificChar) {
          int count = 0;
          for (int i = 0; str[i] != '\0'; i++) {
              if (str[i] == specificChar) {
                 count++;
              }
          }
          return count;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User Kupo's Avatar
    Join Date
    Dec 2001
    Posts
    36
    Thank you now, to figure it all out.. *sigh* newbieism

  4. #4
    Registered User Kupo's Avatar
    Join Date
    Dec 2001
    Posts
    36
    Okay, I probably sound really stupid, but if you could put that code into context, it would really help. Just a small program to like, take how many L's in a string would REALLY help

  5. #5
    Unregistered
    Guest
    #include <iostream.h>
    #include <conio.h>

    int countChars(char str[], char letter) {
    int count = 0;
    for (int i = 0; str[i] != '\0'; i++) {
    if (str[i] == letter) {
    count++;
    }
    }
    return count;
    }


    int main()
    {char string [];
    char letter;
    cout<<"Input String";
    cin>>string;
    cout<<"Letter to look for";
    cin>>letter;
    cout<<letter<<" appeared "<<countChars(string[],letter) <<"times.";

    return 0;}

  6. #6
    Registered User Kupo's Avatar
    Join Date
    Dec 2001
    Posts
    36
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 09:33 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM