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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

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

    I've written a program to read a string from the UNIX command line, and repeat or reverse it depending on the command line options it's given, and it seems to be working correctly apart from for some reason printing ".N=?" (without quotes, and question mark in a bold hexagon) at the beginning of my string.

    here is my code:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) {	
    
    char option;				
    char string[100];			//To store string being processed
    char rstring[100];			//To store reverse string
    
    int n,i;				//n for number of repeats, i for iteration of loop
    opterr=0;				//Disable standard error messages 
    
    while((option=getopt(argc,argv,"hr:b"))!=-1){
    	printf("string=%s\n",string);	
    	i=1;							//skip parsing of program name	
    	while(i<argc){						//parse through strings 
    		if(!strcmp(argv[i],"-r")){ 			//if r option found...
    			i+=2;					//... skip option and argument
    		}else{
    			if(!strcmp(argv[i],"-b")){		//if b option found...
    				i++;				//...skip option
    			}else{
    				strcat(string,argv[i]);		//otherwise, add current string to string
    				strcat(string," ");		//add space between words of string		
    				i++;				//increment counter, move to next string		
    			}
    		}
    	}	
    
    /*then I have a switch statement to take the command line options and perform the 
    functions on the string, which I know work as I've tested seperately. 
    The printfs above are for debugging.
    This part of the code is just for reading the string from the command line (ignoring any options there may be), but for some reason it puts the ".N=?" at the beginning of the string. This happens even with the first printf before the nested if statements.

    Anyone know whats wrong?
    Last edited by bertazoid; 02-08-2009 at 02:33 PM.

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, 08:15 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM