-1

This is the first part of the program and I have a few questions on how parts of it work exactly. Keep in mind this is the first C program I have written. scanf("%d",&numberOfTimes); Why do I need the & and what does it do? char input[][200]; Is this basically an array of strings or is it something completely different?

#include <stdio.h>
#include <string.h>
char outputs[100];
char input[][200];
int numberOfTimes;

void io(void){
    scanf("%d", &numberOfTimes);
    for(int i = 0; i < numberOfTimes; i++){
        scanf("%s",input[i]);
    }
}

This next part of the code is my attempt at actually solving the problem however I suspect that I screwed up the use of a function but I don't know which one or I used something improperly in order to get this result. (I provided example i/o of me code at the bottom).

void stringManipulation(char string[200]){
    int strLength = strlen(string);
    int number = strLength/2;
    for(int i = 0; i <= number; i=i+2){
        strcat(outputs,&string[i]);
    }
}



int main(void) {
    io();
    for(int i = 0; i < numberOfTimes; i++) {
        stringManipulation(input[i]);
        printf("%s\n",outputs);
        memset(&outputs[0], 0, sizeof(outputs));
    }
    return 0;
}

Did I use memset properly? Again I don't understand the use of the &.

Example input:

4

your

progress

is

noticeable

Expected output:

y

po

i

ntc

Output I am getting:

yourur

progressogressress

is

noticeableticeableceable

Thank you for your help.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • `char input[][200];`, the compiler should complain – M.M May 25 '16 at 03:12
  • It did complain but it still worked. – kar3154 May 25 '16 at 03:15
  • Results of running any program with errors are meaningless. For example in your case you got a bunch of garbage output, but anything could have happened. – M.M May 25 '16 at 03:17
  • Ok I understand what you mean. I thought it would be ok because they were just warnings about that. – kar3154 May 25 '16 at 03:39
  • It might ne helpful to realize that C has neither strings (it has pointers to start of character strings with the first byte value 0 indicating end of string, with no regard for how much space there is for the string) nor arrays (it has pointers to raw memory buffers, with just enough of static typing glued on top so you can talk about array types). This important to realize for those C learners, who already know other languages which have strings and arrays. – hyde May 25 '16 at 04:55

2 Answers2

0

The & before a variable means that you are referring to the address of the variable, and not the variable value itself. If you don't know about what the address of a variable is : http://www.cquestions.com/2010/02/address-of-variable-in-c.html

char input[][200] is an array of char array (a char array is vulgarized as a string but it ISN'T the TYPE).

Your problem is about your strcat use, you're adding characters that are between string[i] (included) and string[strLength], not only the string[i] character.

Sorikairo
  • 798
  • 4
  • 19
0

Your problem is in strcat:

Your code passes the address and hence you are getting the entire string from that character onward.

strcat(outputs,&string[i]);

Now, change the above code as below:

strcat(output,string[i]);

You'll get the desired output. The problem with your initial code was that you were passing the address and not individual character. Also, change the for loop in such a way that "<"number and not "<="number. Let me know if you have any more doubts.

Deepak S
  • 45
  • 1
  • 10
  • When I took out the & I got an "Incompatible integer to pointer conversion passing 'char' to parameter of type 'const char*'; take the adress with &" – kar3154 May 25 '16 at 19:19
  • strcat is used to append string and not single character. So, use the following link on how to append character to string: http://stackoverflow.com/questions/4834811/strcat-concat-a-char-onto-a-string Try, char temp[2]; temp[0] = string[i]; temp[1]='\0'; strcat(outputs,temp); This will work :) – Deepak S May 26 '16 at 03:23