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:
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.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.
Anyone know whats wrong?


