Question

Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

Language is in C. Need help troubleshooting my code

Goal of code:

* Replace strings with a new string.

* Append an s to the end of your input string if it's not a keyword.

* Insert a period at the end, if you have an empty input string do not insert a period

Problems:

//Why does my code stop at only 2 input strings

//If I insert a character my empty string case works but it's still bugged where no string is printed at all besides the period.

//Edge case for consecutive key words? Doing str1 str1 results in just "is str1 is str1s"

I've rewritten the input strings in case of academic integrity issues.

int main(int argc, char** argv) {

    int counter = 0;

    for(int i = 0; i

    counter = i;

        if(strcmp(argv[i],"str1")==0){

            printf("%s", "is str1 ");

            if(strcmp(argv[i+1],"str2")==0){

                printf("%s\n", "is str2");

            }

            else if(strcmp(argv[i+1],"str3")==0){

                printf("%s\n", "is str3");

            }

            else if(strcmp(argv[i+1],"str 4")==0){

                printf("%s\n", "is str4");

            }

            else{

                    printf("%s\n",strncat(argv[i+1],"s",1));

            }

        }

        if (*argv[0] != '\0'){

            printf("%s\n", ".");

        }

        */

        printf("%d\n",counter);

    }   

    return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I didn't get much of your program but the issue I found is indexing.

argv[0] holds the name of the program and argv[1] points to the first command line argument and argv[n] gives the last argument. If no argument is supplied, argc will be 1.

Output

Code

#include <stdio.h>

#include <stdlib.h>

#include<string.h>

int main(int argc, char **argv)

{

   if(argc<=1){

      printf("No arguments provided!!\n");

      return 0;

   }

   int count = argc-1;

   if(strcmp(argv[1],"str1")==0){ //checking equal to str1

      printf("str1 found!!");

   }

   if(count>1){ //concat s

      printf(strcat(argv[2],"s"));

      printf("\n");

   }

   if(count>2){

      if(argv[3][0]!='\0'){ //concat dot

         printf(strcat(argv[3],"."));

         printf("\n");

      }

   }

   if(count>3){

      printf("%s\n",argv[4]);

   }

   if(count>4){

      printf("%s\n",argv[5]);

   }

   if(count>5){

      printf("%s\n",argv[6]);

   }

   return 0;

}

Add a comment
Know the answer?
Add Answer to:
Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • I don't know how to run this code on visual studios and I don't understand why...

    I don't know how to run this code on visual studios and I don't understand why the answer is 211111118. Please explain! Thank you! CONSIDER THE FOLLOWING CODE: void Question() {       string x = "12";       mystery1(x);       string str1 = "21";       mystery2(str1);       string str2 = "11";       mystery2(str2);       string str3 = "31";       mystery2(str3);       string str4 = "91";       mystery2(str4);       string str5 = "81";       mystery1(str5);       x = x + str1 +...

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • Can someone help me out with this? You are given a program that receives four lines...

    Can someone help me out with this? You are given a program that receives four lines in the below format, and stores them in str1, str2, str3, and num1. This is not a very long sentence. is long 4 Expand this program to: Write an if-elseif-else statement to print this line only if num1 is higher than 0: "Num1 is higher than 0!" print this line only if num1 is 0: "Num1 equals to 0!" And otherwise, print: ""Num1 is...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Note that the main function that I have provided does use <string.h> as it constructs test...

    Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not *...

  • CSC Hw Problems. Any help is appreciated I dont know where to start let alone what...

    CSC Hw Problems. Any help is appreciated I dont know where to start let alone what the answers are. Your assignment is to write your own version of some of the functions in the built-in <string.h> C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT