Thread: switch a word

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    switch a word

    So the thing is you have to put in a name, like "wtr".
    Then you have to check what's the length of the word.


    Now we have to switch the other word, so new_worth have to become : rtw.

    But my problem is after the loop to switch the word its still the same as wtr.

    I think it must be made with pointers? But can anyone help me?

    Code:
    #include "stdafx.h"
    #include <string.h>
    #define AANTAL 8
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    	int i;
    	int positie = 0;
    	char word[AANTAL];
    	char new_word[AANTAL];
    	int ptr;
    	printf("Put a word here: ");
    	scanf("%s",&word);
    	int length = strlen(word);
    	strcpy(new_word,word);
    	printf("%s",new_word);
    
    	for(i=length; i>= 0;i--){
    		scanf("%c",&new_word[i]);
    	}
    
            //The problem must be here, new_word is still the same as before...
    
    
    	printf("%s",new_word);
     
            //here i wanted to check iff the words are the same or not...
    
    	ptr = strcmp(new_word,word);
    	if(ptr == 0){
    		printf("They are the same");
    	}else{
    		printf("They are not the same");
    	}
    
    	scanf(" ");
    	return 0;
    }
    Last edited by Salem; 08-03-2010 at 11:45 AM. Reason: Use [code][/code] tags for CODE!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Wtr!

    I thought you were in the wrong forum, but I see your code is nearly all straight C, so good deal.

    instead of using quote tags for your code though - use code tags - the # icon right next to the quote tag icon in the advanced editor window.

    Just a few suggestions:

    1) strcmp() returns an int, not a pointer.

    2) you want to use fgets() to enter a string into an array:
    fgets(arrayName, sizeof(arrayName), filePointer (or just stdin));

    Look ma! No over-runs!

    3) take out the ampersand & before word in scanf(). Word is the address to the base of it's own array.

    4) When you use scanf() to get a char, you have to be REALLY careful. Remember that when you hit enter key, you leave a newline char in the keyboard buffer - scanf() doesn't remove it. No problem for numbers, scanf() jumps over newlines as "whitespace". Not so for char's - put this, after your scanf() line, inside the loop:
    Code:
    getchar();
    and that will pull the newline char left behind by scanf(), off the keyboard buffer, and quit mucking up your input with scanf();

    You should also add a getchar() line up above the first scanf() for a char, as well.

    Common problem, and you may have others. This is just what I see looking right now.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    ye oké, i know its return a integer...

    But anyway, do you know how to do it? I must check iff the new_word is the same as word...

    But the problem is i have to save new_word to the switch word iff you understand me? my english isnt so good.

    First i need to read in the word like wtr -> WORD : WTR
    then i have to switch it -> NEW_WORD : RTW

    and then check iff the words are the same in this case it's not...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  2. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 06:13 PM
  3. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 06:13 AM
  4. Word Count
    By simple in forum C Programming
    Replies: 12
    Last Post: 10-04-2002, 10:47 PM
  5. Help with a troublesome C++ program
    By Kristina in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2002, 01:28 PM

Tags for this Thread