Help on string program please

This is a discussion on Help on string program please within the C Programming forums, part of the General Programming Boards category; This program is to take in a number of words, reverse them and compare the reverse word to the original ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    6

    Help on string program please

    This program is to take in a number of words, reverse them and compare the reverse word to the original and print how many times they differ by character for example racecar in reverse would differ 0 times and "toy" would differ twice... i know i am close. here is what i have.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    int main(){
        //declare variables and assign arrays for strings
        char str[50];
        char rev[50];
        int k,change,n,i=-1,j=0;
    
    
    
    
    
    
        //Inpute number of words
        printf("How many words?\n");
        scanf("%d", &n);
    
    
        //input words
        printf("Enter any %d words :\n",n);
        for(i=0; i<n; i++){
        scanf("%s",&str);
    
    
    
    
        // take in string and reverse it by character
        while(str[++i]!='\0');
    
    
        while(i>=0)
         rev[j++] = str[--i];
    
    
        rev[j]='\0';
    
    
        for(k=0; k<strlen(str); k++){
            if(str[k] != rev[k])
            change++;
        }
    
    
        //print reversed word
        printf("The word %s differs from its reverse in %d places\n",str,k);
    
    
        change=0;
        }
    
    
    
    
    
    
    
    
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    22
    Problems:

    1) Change is incremented before it is initialized
    int change = 0; when defining is necessary (or set it to 0 before using it at some point)

    2) Your final printf is using k instead of change! Thus printing the number of characters in the string rather than the number of character differences.

    Questions:

    1) Do you have to create a new array of the reversed string? or can you simply compare back-to-front?
    Example:
    Code:
            int len = strlen(str);
            int front = 0, back = len -1;
            while(front <= back)
            {
                if(str[front] != str[back])
                {
                    change++;
                }
                front++;
                back--;
            }
    Probably doesn't matter here but will make a difference with very large strings.

    2) You only request input once maybe you should put a "Enter a word:" printf statement into your for-loop before the scanf. Just a suggestion though.
    Last edited by Tomwa; 03-21-2013 at 07:35 PM.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    Quote Originally Posted by Tomwa View Post
    Problems:

    1) Change is incremented before it is initialized
    int change = 0; when defining is necessary (or set it to 0 before using it at some point)

    2) Your final printf is using k instead of change! Thus printing the number of characters in the string rather than the number of character differences.

    Questions:

    1) Do you have to create a new array of the reversed string? or can you simply compare back-to-front?
    Example:
    Code:
            int len = strlen(str);
            int front = 0, back = len -1;
            while(front <= back)
            {
                if(str[front] != str[back])
                {
                    change++;
                }
                front++;
                back--;
            }
    Probably doesn't matter here but will make a difference with very large strings.

    2) You only request input once maybe you should put a "Enter a word:" printf statement into your for-loop before the scanf. Just a suggestion though.
    thank you for the response! it can be front to back really doesnt matter. Just has to be able to read back the right amount of differences

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    OK now i feel like ive moved the brackets EVERYWHERE but for some reason it is only getting the number of "differences" in the first word and then adding on to the other words can someone help locate my error?

    Code:
     
    
    #include<stdio.h>
    #include<string.h>
    
    
    int main(){
        //declare variables and assign arrays for strings
        char str[50];
        char rev[50];
        int n,i,k,change=0;
        int len = strlen(str);
        int front = 0, back = len -1;
    
    
    
    
        //Inpute number of words
        printf("How many words?\n");
        scanf("%d", &n);
    
    
        //input words
        printf("Enter any %d words :\n",n);
        for(i=0; i<n; i++){
        scanf("%s",&str);
    
    
    
    
        // take in string and reverse it by character
        while(front <= back)
    {
        if(str[front] != str[back])
        {
            change++;
        }
        front++;
        back--;
    }
    
    
        for(k=0; k<strlen(str); k++){
            if(str[k] != rev[k])
            change++;
    
    
        }
    
    
        //print statement telling the user how many times the letter changes
    
    
        printf("The word %s differs from its reverse in %d places\n",str,change);
    
    
    
    
        }
    
    
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to reset the variables front,and change for each word you check - so line 27 would be a good place to reset them, to zero. The back variable also needs to be set to the strlen(str), before the while loop on line 30.



    The change variable shouldn't be a part of the while loop starting on line #30. You've got that correctly handled by the for loop AFTER the word has already been reversed. You need however, to reset change to zero, before the program drops into that final for loop, and starts counting the change for this word.

    It would be a LOT easier for you, if you just indented your code - errors like this leap out at ya, before long.
    Last edited by Adak; 03-22-2013 at 12:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help about string in c program
    By raihan004 in forum C Programming
    Replies: 2
    Last Post: 09-15-2012, 06:54 AM
  2. String Program
    By ishwariamca in forum C++ Programming
    Replies: 11
    Last Post: 09-18-2008, 12:32 AM
  3. Help with string program
    By Duskan in forum C Programming
    Replies: 8
    Last Post: 04-02-2007, 09:27 AM
  4. String program
    By howdy_doody in forum C Programming
    Replies: 2
    Last Post: 03-13-2005, 07:01 PM
  5. need help with C-string program
    By fritz83 in forum C++ Programming
    Replies: 10
    Last Post: 12-02-2003, 12:32 AM

Tags for this Thread


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