You are on page 1of 11

Lecture 05

We will cover these skills:


 String in C/C++
 String Functions & purpose

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:

Example: Dynamic memory allocation


char str []= "Hello";

Index 0 1 2 3 4 5
Varıable H e l l o \0

Example: static memory allocation


char str [10]= "Hello";
Index 0 1 2 3 4 5 6 7 8 9
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++ :

#include <iostream> Output

int main() { c/c++ programming

char str[20]="c/c++ programming";


printf ("%s", str);
return 0;
}

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++ :

The alternative functions for read an& print:


 fgets() //for reading
 puts() //for printing

#include <stdio.h> Output


int main()
{ Enter full name: uskudar university
char name[20]; Your name is: uskudar university
printf("Enter full name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Your name is: "); //printf("Your name is: %s\t", name);
puts(name);
return 0;
}

DR.KALAN 7
C /C++ Programming
String Functions :
In order to use strıng functıon sw need #include <string.h>

strlen(s1); Returns the length of string s1.

strcpy(s1, s2); Copies string s2 into string s1.

strcat(s1, s2); Concatenates string s2 onto the end of string s1.

strcmp(s1, s2); Returns 0 if s1 and s2 are the same; -1 if s1<s2; 1 if s1>s2.

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

printf("\n---- copy str1 into str3 ----");


strcpy(str3, str1);
printf("\n strcpy( str3, str1) : %s", str3 );

printf("\n---- concatenates str1 and str2 ----");


strcat( str1, str2);
printf("\nstrcat( str1, str2): %s", str1 );
//printf("\n--- adding space in strcat ----");
//strcat(strcat(str1, " "),str2);
//printf("\nstrcat( str1, str2): %s", str1 );
return 0;
}

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 () {

char str1[20] = "A";


char str2[20] = "a";

printf("Result of comparing str1 and str2 is: %d", strcmp(str1, str2));

return 0; Note: ASCII code ‘A’ is small than ‘a’, so


} the result is -1

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;

// Find first occurrence of str2 in str1


p = strstr(str1, str2);

if (p) {
strcpy(p, "teacher");
printf("%s", str1);
}
else
printf("String not found\n");

return 0;
}

DR.KALAN 11

You might also like