Thread: string problem !!!!!!

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    12

    string problem !!!!!!

    thise is the code!
    its give me an error on the running stage.

    Code:
     #include "stdafx.h"#include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    
    int main()
    {
    char _str[100];
    int i=0,count_str=0;
    printf("enter string up to 10 letters!");
    gets (_str);
    count_str=strlen(_str);
    
    
    
    
    
    
    //for (i=0;i<10;i++){
    //    scanf("%s",&_str[i]);
    //}
    for(i=(count_str-1);i>=0;i--)
    printf("%s\n",_str[i]);
    
    
    
    
    
    
    
    
    getch();
    
    
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    everything seems to be ok but still ,not working.
    iam lost.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In printf() format, you are using %s, but giving it the parameter of a char _str[i]. Also, you're printing the string backwards, is that what you want, kind of a "stepped" print out:

    Code:
    e
    ge
    rge
    orge
    eorge
    George
    Is what I mean by "stepped". Is that what you want?

    Lastly, variables with a leading underline, and double underline, are usually reserved for the compiler, and shouldn't be used by the programmer, at all.

    Do you want to print the string out backwards? And how many times? The entire string each time, or "stepped" in length?
    Last edited by Adak; 01-09-2013 at 07:26 AM.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    i need to scanf string and yes print it backwards one time .

    thank you very much for your quick answer

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    no sir,

    it goes like thise ...


    good

    d
    o
    o
    g

    or

    d o o g

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("%s\n",_str[i]);
    Use %c to print individual characters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Read the string with
    Code:
    scanf("%s",...);
    Never use gets!!!
    Then you have in the buffer the string.
    In a loop start printing every character of it, starting from the end of it.
    Where is the end of the string?
    Use strlen to find out. Be careful of the null terminator of the strings.
    In the loop, you will have your counter decreasing until it reaches the zero (including)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    its working ....tankns a loot.
    Code:
      #include "stdafx.h"#include #include #include #include int main(){char _str[100];int i=0,count_str=0;printf("enter string up to 10 letters!");gets (_str);count_str=strlen(_str);//for (i=0;i=0;i--)printf("%c\n",_str[i]);getch();return 0;}

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    Code:
     #include "stdafx.h"#include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    
    
    int main()
    {
    char _str[100];
    int i=0,count_str=0;
    printf("enter string up to 10 letters!");
    gets (_str);
    count_str=strlen(_str);
    
    
    
    
    
    
    //for (i=0;i<count_str;i++){
    //    scanf("%c",&_str[i]);
    //}
    for(i=(count_str-1);i>=0;i--)
    printf("%c\n",_str[i]);
    
    
    
    
    
    
    
    
    getch();
    
    
    return 0;
    
    
    }

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    great forum...thanks again (-:

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Is post #7 not visible?????
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    soon i will start studying about pointers.
    thanks you saved me.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I refreshed it once, and did not see several posts in this thread. After refreshing twice more, I see them all, and that it's solved.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You should listen to std10093 and stop using gets().
    We'll keep reminding you each time you use gets(), so it's best to stop using it. It is a completely unsafe function with no place in modern programs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    12
    i will thanks for the advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-10-2011, 05:25 AM
  2. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  3. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  4. Replies: 0
    Last Post: 04-05-2003, 09:33 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM