0

i have started coding in c but i keep getting errors i don,t know how to solve maybe it is because i am used to coding in php language but i hope anybody can help me

#include <stdio.h>
#include <cs50.h>
#include <string.h>

int main(int argc , string argv[])
{

string tekst[] =  0 ;

string plain = get_string("plain text: ")
;
string encripted = 0;

string plaintext[] =  plain;

string alphabet = "abcdefghijklmnopqrstuvwxyz";
int count = 0;

for (int i = 0; i < strlen(plain); i++ ){

        for(int x = 0; x <strlen(alphabet); x++){

            if(plain[i] == alphabet[x]){

                tekst[i] = argv[x];



            }
        }
    printf("%s",*tekst);
    }
}

if i run this it will give me

substitution.c:8:8: error: array initializer must be an initializer list string tekst[] = 0 ;

substitution.c:14:8: error: array initializer must be an initializer list string plaintext[] = plain;

i tried google but doens,t help me

0

First, there's this:

string tekst[] =  0 ;

This is declaring array tekst, but it isn't setting a size. Even if it were, a string is in double quotes. (To be honest, I've never researched what happens when an actual integer is attempted to be assigned to a string.) Also, in C, string length is immutable. Once the string size is set, it can't be changed. The memory space is fixed. You can store shorter strings in that space, but not longer ones.

To set the size, it has to be either explicitly set [in the brackets] or implicitly by assigning a string in double quotes.

In short, the compiler doesn't like it. The code needs to give a clue about the string length.

Next, string plaintext[] = plain;

In C, you can't copy a string between vars with the "=" assignment operator. At best, this would copy a pointer address between pointers to strings. In reality, it just fails.

To copy a string, you need to use a call to strcpy() or one of it's cousins.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .