Thread: simple string question

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    9

    simple string question

    i have written the following prog. for reversing a string,output is showing nothing after the 'the reversed stringis'
    plz have a look and rectify my errors
    #include<stdio.h>
    # include<string.h>

    void main()
    {
    char source[20];
    char*str;
    char str1[20];
    int i,j=0;
    clrscr();
    printf("enter your city");
    scanf("%s",source);
    i=strlen(source);
    printf("%d",i);
    str=source[i-1];


    while(j<=i)
    {

    str1[j]=*str;

    j++;str--;
    }
    str1[j]='\0';
    printf("the reversed string %s",str1);

    getch();
    }


    and wud somebody plz explain me the concept of dummy operartors

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    6
    Not sure if this is the only error because I don't use scanf() and printf() much because I mainly do embedded stuff, but the line before the while should be

    &source[i-1].

    str is a pointer so you need to put the character's address in there, not the character.
    Make sure that scanf gives you a null terminated string as well otherwise strlen will not work.

    Davey

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >output is showing nothing after the 'the reversed stringis'
    Without looking closely at your code, I would guess that you are copying the null character into index 0 of the array. This makes it an empty string which will print nothing.

    I would recommend comparing your code to the following. Try to determine why I chose to do certain operations differently. Using the information on these forums and the FAQ provided on this web site, you can do so easily.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char str[BUFSIZ];
    
      printf("Enter a string: ");
      fflush(stdout);
      if (fgets(str, sizeof str, stdin) != NULL) {
        size_t s, e;
        char *newline = strchr(str, '\n');
    
        if (newline != NULL) {
          *newline = '\0';
        }
        for (s = 0, e = strlen(str) - 1; s < e; s++, e--) {
          char save = str[s];
          str[s] = str[e];
          str[e] = save;
        }
        printf("The reversed string is: \"%s\"\n", str);
      }
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    9
    thanx davey,
    that worked, i missed the &source[i-1],
    so whats the purpose of dummy variables in the progs.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>so whats the purpose of dummy variables in the progs.
    Your question is too vague. Please describe the context in which these dummy variables are used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Simple string question?
    By Cstudent2121 in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 11:47 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM