Like Tree1Likes
  • 1 Post By CommonTater

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

This is a discussion on How to find out the string size which includes more than a word within the C Programming forums, part of the General Programming Boards category; #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 ...

  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
    22,129
    Use fgets to read in the string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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...
    GReaper likes this.

  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 07: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 08: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, 08:41 PM
  2. Find whole word in string, case insensetive.
    By IM back! in forum C++ Programming
    Replies: 12
    Last Post: 03-19-2010, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21