program printing ".N=?" at beginning of string

This is a discussion on program printing ".N=?" at beginning of string within the C Programming forums, part of the General Programming Boards category; post the relevant code...

  1. #16
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    post the relevant code

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
    
    char string[]="hello world";
    char string2[100];
    int i;
    
    for(i=0;i<=strlen(string);i++){
    	printf("strlen(string)-i= %d-%d = %d\n",strlen(string),i,strlen(string)-i);
    	string2[i]=string[strlen(string)-i];
    	}
    printf("string=%s\n",string);
    printf("rstring=%s\n\n",string2);
    
    return 0;
    }
    Output:

    strlen(string)-i= 14-0 = 14
    strlen(string)-i= 14-1 = 13
    strlen(string)-i= 14-2 = 12
    strlen(string)-i= 14-3 = 11
    strlen(string)-i= 14-4 = 10
    strlen(string)-i= 14-5 = 9
    strlen(string)-i= 14-6 = 8
    strlen(string)-i= 14-7 = 7
    strlen(string)-i= 14-8 = 6
    strlen(string)-i= 14-9 = 5
    strlen(string)-i= 14-10 = 4
    strlen(string)-i= 14-11 = 3
    strlen(string)-i= 14-12 = 2
    strlen(string)-i= 14-13 = 1
    strlen(string)-i= 14-14 = 0
    string=I am a string!
    rstring=
    Last edited by bertazoid; 02-08-2009 at 03:46 PM.

  3. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    I think I got it. It's because it's putting the null termintor of string as the first element of string2.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    that's exactly it

  5. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    I changed the for loop to start at i=0, so it would exclude the null character at the end of the string, but my program still gives the same output - rstring is blank.

  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    try this (starting a line 12):
    Code:
     if (i>0) string2[i-1]=string[strlen(string)-i];
             }
    string2[i]='\0';
    rstring=dlrow olleh
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Thanks it worked, except instead of using the if statement, I just started the loop at i=1.

  8. #23
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    IMO you are getting confused between the length of string[] and the char stored at a particular index of the array string[].
    Code:
    string="I am a string!";
    strlen(string)-i=14-0=14;  /* i = 0 */
    strlen(string)-i=14-1=13;  /* i = 1 */
    string[14] = NULL;         /* char at index 14 is NULL */
    string[13] = '!';          /* char at index 13 is '!' */
    So setting i=1 is the way to go.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Help with string program
    By Duskan in forum C Programming
    Replies: 8
    Last Post: 04-02-2007, 08:27 AM
  3. Replies: 8
    Last Post: 03-31-2006, 07:15 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

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