Intro To Algorithm and Programming 05 1
Intro To Algorithm and Programming 05 1
REZA KALAN 1
C /C++ Programming
String in C/C++ :
Strings are used for storing text.
Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated
string contains the characters that comprise the string followed by a null.
string str = {‘H’, 'E’, ‘L’, ’L’, ‘O’, ’\0’};
String in memory
Index 0 1 2 3 4 5
.
Varıable H E L L O \0
DR.KALAN 2
C /C++ Programming
String in C/C++ :
A string variable contains a collection of characters surrounded by double quotes:
Index 0 1 2 3 4 5
Varıable H e l l o \0
DR.KALAN 3
C /C++ Programming
String in C/C++ :
#include <iostream>
Example 1: this code does not work, because there is
not enough space for “hello”. String ”hello” by itself is int main() {
5 character, It also need a ‘\0’ at the end of string.
Therefore we need at least 6 free space in memory char str[5]="HELLO"; // invalid, no enough space allocated
printf ("%s", str);
return 0;
}
#include <iostream>
int main() {
Example 2: this code works, because there is enough
space for “hello” char str[6]="HELLO"; // valid, enough space allocated
printf ("%s", str);
return 0;
}
DR.KALAN 4
C /C++ Programming
String in C/C++ :
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Varıable c / c + + p r o g r a m m i n g \0
Note: strings ( as well Array) are not support the assignment operator once it is declared.
For example,
char c[100];
c = "C programming"; // Error! array type is not assignable.
DR.KALAN 5
C /C++ Programming
String & I/O Operation in C/C++ :
#include <stdio.h>
int main()
We can use scanf to read a string, but it can only read strings {
up to the first space char name[20];
printf("Enter a name: ");
scanf("%s", name);
printf("Your name is %s:", name);
return 0;
}
Example: Output
The output is only print first name. It's because there was a space Enter a name: Uskudar university
after Uskudar. Your name is: Uskudar
Also notice that we have used the code name instead of &name
with scanf().
DR.KALAN 6
C /C++ Programming
String & I/O Operation in C/C++ :
DR.KALAN 7
C /C++ Programming
String Functions :
In order to use strıng functıon sw need #include <string.h>
strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
DR.KALAN 8
C /C++ Programming
String Functions : #include <stdio.h> #include <string.h> Output
int main () {
Example 1: char str1[15] = "Hello"; char str2[15] = "World"; strlen(str1) : 5
char str3[20]; int len ; ---- copy str1 into str3 ----
strcpy( str3, str1) : Hello
len = strlen(str1); ---- concatenates str1 and str2 ----
printf("strlen(str1) : %d", len ); strcat( str1, str2): HelloWorld
DR.KALAN 9
C /C++ Programming
String Functions :
Example 3:
Output
#include <stdio.h>
#include <string.h> Result of comparing str1 and str2 is: -1
int main () {
DR.KALAN 10
C /C++ Programming
String Functions : #include <stdio.h> Output
#include <string.h>
Example 2: He is a teacher
int main () {
char str1[30] = "He is a student";
char str2[10] = "stude";
char *p;
if (p) {
strcpy(p, "teacher");
printf("%s", str1);
}
else
printf("String not found\n");
return 0;
}
DR.KALAN 11