Thread: need help involving strings and stuff

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    Fukushima-shi, Fukushima, Japan, Japan
    Posts
    6

    need help involving strings and stuff

    im still a student in computer science, so my code may look a bit bad

    my problem is this: i got a code which prints the reverse of a string(eg. input: qwerty output: ytrewq) and when i run it alone in a seperate .c file as the main function, it works fine, but when i include it in my main .c file(which also includes a lot more functions) it goes haywire and prints additional characters in the output.

    heres the code
    Code:
    char s[100];    
        int i, j = 0, x = 0;
        printf("\ns? ");
        scanf("%s", &s);
    
    
        while(s[x] != '\0')
        {
            j++;
            x++;
        }
        i = 0;
        while(i < j)
        {
            s[i] = s[i] + s[j];
            s[j] = s[i] - s[j];
            s[i] = s[i] - s[j--];
            i++;
        }
        printf("\nReverse string is :%.2s", s+1);
    it goes fine alone as the main function but when i put it in my main code which looks like this:

    Code:
    #include<stdio.h>
    void reverse();
    
    void main()
    {
        int function; 
    
        printf("Enter function number: ");
        scanf("%d", &function);
    
        switch(function)
        {
            case 1:
            {
                reverse();
                break;
            }
    }
    
    void reverse()
    {
        char s[100];
        int i, j = 0, x = 0;
        printf("\ns? ");
        scanf("%s", &s);
    
    
        while(s[x] != '\0')
        {
            j++;
            x++;
        }
        i = 0;
        while(i < j)
        {
            s[i] = s[i] + s[j];
            s[j] = s[i] - s[j];
            s[i] = s[i] - s[j--];
            i++;
        }
        printf("\nReverse string is :%s\n", s+1);;
    }
    idk if the second code works because it doesnt realy look like that but i hope you get the point.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    1. You're using scanf with "%s" which expects char* but then you give it the address to your character array, which is char**. Just use
    Code:
    scanf("%s", s);
    2. What is x being used for?
    3. Don't use "%.2s" because it will only print 2 chars of your result.
    4. Major problem--> Why do you use s+1? It seems like you want to skip the '\0' that was at the end of the old string, but where is the '\0' at the end of s+1?

    For example if you type "qwerty" then the memory in s looks like this:

    Before: q, w, e, r, t, y, \0

    After: \0, q, w, e, r, t, y

    So I guess you thought you can just skip over the '\0' by printing s+1. However, there is not necessarily a '\0' at the end of 'y'. Maybe there will be some of the time, maybe there won't be some of the time. Solution is to back up your j index by one as soon as it sees the '\0'

    Code:
    while(s[j] != '\0')
        {
            j++;
        }
        j--;
    
        i = 0;
        while(i < j)
        {
            s[i] = s[i] + s[j];
            s[j] = s[i] - s[j];
            s[i] = s[i] - s[j--];
            i++;
        }
    Now it will work like this:

    Before: q, w, e, r, t, y, '\0'
    After: y, t, r, e, w, q, '\0'

    Then forget about the s+1
    Code:
    printf("Reverse string is: %s\n", s);

  3. #3
    Registered User
    Join Date
    Dec 2012
    Location
    Fukushima-shi, Fukushima, Japan, Japan
    Posts
    6
    thanks man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers with strings and stuff.
    By DLH112 in forum C Programming
    Replies: 9
    Last Post: 10-21-2009, 04:51 PM
  2. Replies: 23
    Last Post: 05-11-2008, 07:12 AM
  3. Creating Strings from other stuff
    By Manitoadlet in forum C++ Programming
    Replies: 7
    Last Post: 09-10-2002, 06:07 PM
  4. Strings and stuff
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2002, 03:36 PM
  5. Strings and stuff
    By casanova0o7 in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2002, 08:37 PM