C++ Note Array and String
C++ Note Array and String
return(0);
}
Computer Programming 3/20/2021 6
▪ Using an array initializer, Splitting the declaration
and initialization parts would cause a syntax error.
▪ Thus, the next statement is wrong:
double myList[4];
myList = {1.9, 2.9, 3.4, 3.5};
▪ C++ allows you to omit the array size when declaring
and creating an array using an initializer.
▪ For example, the following declaration is fine because
the compiler automatically figures out how many elements
are in the array:
A[2] = 75;
▪ And passing the third element of A value to the variable a,
we could write:
a = A[2];
▪ We can use loop to manipulate/process an array.
return(0);
}
#include <iostream.h>
The strcat(s1,s2) function
#include <cstdio.h>
appends s2 to the end of s1.
#include <cstring.h>
String s2 is unchanged.
int main(){
char s1[21], s2[11];
strcpy(s1, “Hello”);
strcpy(s2, “ there”); Displays:
strcat(s1, s2);
Hello there
cout << s1 << endl;
there
cout << s2 << endl;
return(0);
}
Computer Programming 3/20/2021 22
The first string array has to be large enough to hold both strings:
▪ To be on the safe side:
strlen(s1concats2) >= strlen(s1) + strlen(s2)