0

Hope you all doing great!

Here is what I'm trying to do - I would like to create a program which will save a string with up to 50 symbols (or less) to an array and then print out each array symbol. The problem is that there is no such method like .append in C. How can I solve this issue if I don't know the exact length of a string?

Any examples or suggestions are more than welcome. P.s. I'm quite new to C programming (previously used Java, JavaScript and etc), so please just bear with me:)

Den
  • 53
  • 2
  • 8
  • What do you mean by "save"? Will you read character by character, and append to the array? Will you read strings and append? Will you have a static compile-time fixed string you want to put into your array? And what have you tried? Perhaps you need to just spend some time studying C (reading a couple of books or a few tutorials)? – Some programmer dude Dec 01 '18 at 12:34
  • Possible duplicate of [writing formatted data of unknown length to a string (C programming)](https://stackoverflow.com/questions/1854353/writing-formatted-data-of-unknown-length-to-a-string-c-programming) – roschach Dec 01 '18 at 12:36
  • @Someprogrammerdude I have to read a string and each character of this string should be added to the array one by one. – Den Dec 01 '18 at 12:38

3 Answers3

1

The idea regarding '\0' in wizzwizz4's answer helped me to solve this issue. However, I have done it in another way:

#include <stdio.h>

int main(){
    char input[50]; 

    printf("%s\n","Enter The String:");
    fgets(input,sizeof(input),stdin);

    for(int i=0; input[i]!='\0'; i++){
        printf("%c",input[i]);
    }
}

It is working great and it's quite short. This is a good, safe approach, if we exclude that the printed string won't have the '\0' itself.

Den
  • 53
  • 2
  • 8
  • Stack Exchange isn't a traditional forum; you shouldn't post answers that aren't answers. I suggest editing it slightly so that it answers the question you've asked. And... yes, this is actually better than my approach! I'll just edit it for you. – wizzwizz4 Dec 01 '18 at 13:41
0

I suggest you keep the length of the string available at all times, in a variable. Alternatively you can use the function strlen().
Most string related functions have a prototype in <string.h>. You need to include that standard header.

#include <string.h>

char text[51] = {0};
int textlen = 0; // should be size_t

// possibly, in other lang: text = "foobar"
strcpy(text, "foobar");
textlen = strlen(text); // 6 in this easy case

// possibly, in other lang: text = text + character
if (textlen < 50) {
    text[textlen++] = character; // add character to text, update textlen
    text[textlen] = '\0'; // remember the terminator
} else {
    fprintf(stderr, "not enough space for all characters.\n");
    exit(EXIT_FAILURE);
}

Read about strings, for example, at chapter 8 of the c-faq

pmg
  • 106,608
  • 13
  • 126
  • 198
-1

In order to tackle this problem, we'll need to know what a C string actually is. Luckily, I already know so I can tell you.

A C string is a sequence of characters that are not '\0', followed by a '\0' character. It can be any length. When we pass strings around, we're (almost) always passing around a pointer to the first character of the string (that is, the position in memory where it's stored). When you work with strings, you carry on until you see '\0'. This makes it really easy to accidentally introduce a security vulnerability, as you'll see.

Now, how do we use strings? The C Standard Library is here to save the day!

#include <string.h>

This magical line means you can now use functions like strcpy!

#include <string.h>
#include <stdio.h>
char array[50];

int main(int argc, char *argv[]) {
    if (argc < 2) return 1;  // Exit early if we don't have an argument.

    strcpy(array, argv[1]);  // Copy first argument

    for (size_t i = 0; i < 50; i += 1) {
        printf("%c", array[i]);
    }
}

Look, it works:

wizzwizz4@myLaptop:~$ ./a.out "Hello this is my string!"
Hello this is my string!^@k£"'l`¬.v'4ka36}'3.^D,2#xf

But... you probably will have spotted the error. What if the string is longer than the space?

wizzwizz4@myLaptop:~$ ./a.out "Hello this is my string that is too long padding... k'57M3'ck~^B345"
Hello this is my string that is too long padding..
I, THE MIGHTY ATTACKER, HAVE OVERWRITTEN PARTS OF MEMORY THAT I SHOULD NEVER HAVE HAD ACCESS TO AND NOW CONTROL YOUR COMPUTER!

Oops... The space in memory just after array just happened to contain some code, and when the attacker told our program to overwrite that code with k'57M3'ck~^B345, it let the attacker in! To fix this, let's use strncpy instead:

#include <string.h>
#include <stdio.h>
char array[50];

int main(int argc, char *argv[]) {
    if (argc < 2) return 1;  // Exit early if we don't have an argument.

    strncpy(array, argv[1], 49);  // Copy first argument

    for (size_t i = 0; i < 50; i += 1) {
        printf("%c", array[i]);
    }
}

Let's run it!

wizzwizz4@myLaptop:~$ ./a.out "Hello this is my string that is too long padding... k'57M3'ck~^B345"
Hello this is my string that is too long padding..f

Success! Except... this "string" doesn't end in a '\0' – it's not really a string. That means that the program will crash if you try to treat the contents of array as a string, because whatever's using the string will read and read other bits of memory until it finds a '\0' or crashes, like so:

#include <string.h>
#include <stdio.h>
char array[50];

int main(int argc, char *argv[]) {
    if (argc < 2) return 1;  // Exit early if we don't have an argument.

    strncpy(array, argv[1], 49);  // Copy first argument

    printf("%s", array);  // Uh oh...
}

Running it...

wizzwizz4@myLaptop:~$ ./a.out "Hello this is my string that is too long padding... k'57M3'ck~^B345"
Hello this is my string that is too long padding..f^D^D^D^D'2mCln My Password is 358231

^F^JESegmentation fault (core dumped)

To work around this, we need to add a final '\0':

#include <string.h>
#include <stdio.h>
char array[50];

int main(int argc, char *argv[]) {
    if (argc < 2) return 1;  // Exit early if we don't have an argument.

    strncpy(array, argv[1], 49);  // Copy first argument
    array[49] = '\0';

    for (size_t i = 0; i < 50; i += 1) {
        printf("%c", array[i]);
    }
}

It's finally safe. Breathe a big sigh of relief!

wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
  • You make it appear that `'\0'` is not part of the string. I say `'\0'` is an integral part of the string. "A sequence of characters that are not `'\0'`" **is not** a string – pmg Dec 01 '18 at 12:51
  • @pmg Sorry; I meant to say this "string" doesn't end in a `'\0'`. You're completely right. – wizzwizz4 Dec 01 '18 at 12:52
  • @DanielTsarin You won't be able to post properly-formatted code in comments. You _can_ do so in [chat] though; if you want to talk about this I (edit: can't) spin up a chatroom for us (you don't have enough rep yet). – wizzwizz4 Dec 01 '18 at 13:36