Thread: How to find out the string size which includes more than a word

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    How to find out the string size which includes more than a word

    #include <stdio.h>
    #include <string.h>
    void main()
    {
    int i=0;
    char str[40];
    printf("Program to know the string size\n");
    printf("Enter a string with more than 2 words: ");
    scanf("%s", str);
    while(str[i]!='\0')
    {
    i++;
    }
    printf("result of while loop %d\n",i);
    printf("result of strlen is %d\n", strlen(str));
    printf("getting wrong output\n");
    }


    it counts size of only one word. but i need size of entire string. please help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use fgets to read in the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's nice to see that the forum upgrade still lets people post without code tags.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    It's nice to see that the forum upgrade still lets people post without code tags.


    Quzah.
    And post twice in a row, too...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nice! I swear that second one wasn't there when I refreshed this the first time. Oh well.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Nice! I swear that second one wasn't there when I refreshed this the first time. Oh well.

    Quzah.
    Yeah, I see in the general section, this seems to be a bug in the forum code...
    Nasty.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Max, this is simple - just change your starting point in the char array. Work from the very end of the array, and decrement your index, until you find the end of string char. Using fgets() as suggested by Laserlight, may make it easy for you.

    Let's say the sizeof(array) is 100. Your index search starts at 99, (the array index will go from 0 to 99), and decrements down to 75 before it finds the end of string char.

    So the last word ends at array[75], making it 76 char's long. (0 through 75 is 76). Draw it out on paper, and it will make sense.
    Last edited by Adak; 04-10-2011 at 06:21 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Max, this is simple - just change your starting point in the char array. Work from the very end of the array, and decrement your index, until you find the end of string char. Using fgets() as suggested by Laserlight, may make it easy for you.

    Let's say the sizeof(array) is 100. Your index search starts at 99, (the array index will go from 0 to 99), and decrements down to 75 before it finds the end of string char.

    So the last word ends at array[75], making it 76 char's long. (0 through 75 is 76). Draw it out on paper, and it will make sense.
    Best way is probably to use fgets(String,BuffSize,stdin); and strlen()...

    But if you want to roll your own strlen function it's pretty easy...
    Code:
    int MyStrlen(char* Str)
      { char* p = Str;   // string pointer
         while(*p++);
         return --p - Str; }
    Last edited by CommonTater; 04-10-2011 at 07:14 PM. Reason: Pinky failed to engage shift key appropriately...

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Darn! I deleted one of the dupe posts. I was wondering if it would delete them both (if it actually counted them as 2 posts, or just one).

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    thanks

    fgets worked thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple regular expression to find word in a string?
    By BC2210 in forum C Programming
    Replies: 1
    Last Post: 03-28-2010, 07:41 PM
  2. Find whole word in string, case insensetive.
    By IM back! in forum C++ Programming
    Replies: 12
    Last Post: 03-19-2010, 10:06 PM
  3. word size of CPU?
    By sarathius in forum C Programming
    Replies: 5
    Last Post: 03-04-2008, 05:43 AM
  4. Word Find
    By malooch in forum C Programming
    Replies: 2
    Last Post: 11-28-2006, 11:11 AM
  5. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 05:38 PM