0% found this document useful (0 votes)
2 views15 pages

String

The document provides an overview of strings in C programming, explaining that strings are arrays of characters terminated by a null character. It includes examples of string input and output using various functions like scanf, gets, and printf, as well as library functions for string manipulation such as strlen, strcpy, and strcat. Additionally, it contains multiple-choice questions to test knowledge on string functions and characteristics in C.

Uploaded by

Mohamod Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

String

The document provides an overview of strings in C programming, explaining that strings are arrays of characters terminated by a null character. It includes examples of string input and output using various functions like scanf, gets, and printf, as well as library functions for string manipulation such as strlen, strcpy, and strcat. Additionally, it contains multiple-choice questions to test knowledge on string functions and characteristics in C.

Uploaded by

Mohamod Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Regular Batch-08 (C Programming)

String in c

In the C programming language, a string is not a built-in data type, but rather a one-dimensional
array of characters terminated by a null character (\0). The C compiler automatically uses
this null terminator as a sentinel value to determine where the text sequence ends in memory.

A string declaration is the process of creating a string variable to store text data in a program.

char str[] = "Hello";

String Input

Using scanf() #include <stdio.h>


int main() {
char str[100];
scanf("%s", str);
printf("%s", str);
return 0;
}
Using gets() #include <stdio.h>
int main() {
char str[100];
gets(str);
printf("%s", str);
return 0;
}

Using fgets() #include <stdio.h>


int main() {
char str[100];
fgets(str, sizeof(str), stdin);
printf("%s", str);
return 0;
}

Complexity IT Job Care Page 1


Regular Batch-08 (C Programming)

String Output

Using printf() #include <stdio.h>


int main() {
char str[] = "Hello";
printf("%s", str);
return 0;
}
Using puts() #include <stdio.h>
int main() {
char str[] = "Hello";
puts(str);
return 0;
}

String Library Function

Function Name Use of Library Function Without Library Function


#include <stdio.h> #include <stdio.h>
#include <string.h> int main() {
Strlen int main() { char str[100];
char str[] = "Hello"; int i = 0;
Used to find the length of a int len = strlen(str); fgets(str, sizeof(str), stdin);
string printf("%d", len); while (str[i] != '\0') {
return 0; i++;
} }
printf("%d", i);
return 0;
}
#include <stdio.h> #include <stdio.h>
#include <string.h>
Strcpy int main() { int main() {
char str1[100] = "Hello"; char str1[100], str2[100];
Used to copy one string char str2[100]; int i = 0;
into another string. strcpy(str2, str1);
printf("%s", str2); fgets(str1, sizeof(str1), stdin);
return 0; while (str1[i] != '\0') {
} str2[i] = str1[i];
i++;
}
str2[i] = '\0';
printf("%s", str2);

return 0;
}

Complexity IT Job Care Page 2


Regular Batch-08 (C Programming)

#include <stdio.h> #include <stdio.h>


#include <string.h> #include <string.h>
Strrev
int main() { int main() {
used to reverse a string. char str[] = "Hello"; char str[100], rev[100];
int i, len = 0;
printf("%s", strrev(str));
fgets(str, sizeof(str), stdin);
return 0;
} while (str[len] != '\0')
len++;

if (str[len - 1] == '\n')
len--;

for (i = 0; i < len; i++) {


rev[i] = str[len - 1 - i];
}

rev[len] = '\0';

printf("%s", rev);

return 0;
}
#include <stdio.h> #include <stdio.h>
#include <string.h> int main() {
char str1[100], str2[100];
Strcmp int main() { int i = 0, result = 0;
char str1[] = "apple"; fgets(str1, sizeof(str1), stdin);
used to compare two char str2[] = "banana"; fgets(str2, sizeof(str2), stdin);
strings lexicographically while (str1[i] != '\0' || str2[i] !=
(dictionary order). printf("%d", strcmp(str1, '\0') {
str2)); if (str1[i] != str2[i]) {
result = str1[i] - str2[i];
Return Values: return 0; break;
} }
 0 → strings are i++;
equal }
 > 0 → first string is
greater printf("%d", result);
 < 0 → first string is return 0;
smaller }

Complexity IT Job Care Page 3


Regular Batch-08 (C Programming)

#include <stdio.h> #include <stdio.h>


#include <string.h>
int main() {
Strcat int main() { char str1[200], str2[100];
char str1[100] = "Hello "; int i = 0, j = 0;
used to join two strings char str2[] = "World";
together (append second fgets(str1, sizeof(str1), stdin);
string at the end of first strcat(str1, str2); fgets(str2, sizeof(str2), stdin);
string).
printf("%s", str1); while (str1[i] != '\0') {
if (str1[i] == '\n') {
return 0; str1[i] = '\0';
} break;
}
i++;
}

i = 0;
while (str1[i] != '\0') {
i++;
}

while (str2[j] != '\0') {


if (str2[j] != '\n') {
str1[i] = str2[j];
i++;
}
j++;
}
str1[i] = '\0';

printf("%s", str1);

return 0;
}

Complexity IT Job Care Page 4


Regular Batch-08 (C Programming)

Some C programming From String

Check palindrome string #include <stdio.h>

int main() {
char str[100];
int i = 0, len = 0, flag = 1;

fgets(str, sizeof(str), stdin);

while (str[len] != '\0') {


if (str[len] == '\n') {
str[len] = '\0';
break;
}
len++;
}

len--;

while (i < len) {


if (str[i] != str[len]) {
flag = 0;
break;
}
i++;
len--;
}

if (flag)
printf("Palindrome");
else
printf("Not Palindrome");

return 0;
}
Remove spaces from #include <stdio.h>
string
int main() {
char str[100], result[100];
int i = 0, j = 0;

fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {


if (str[i] != ' ' && str[i] != '\n') {

Complexity IT Job Care Page 5


Regular Batch-08 (C Programming)

result[j] = str[i];
j++;
}
i++;
}

result[j] = '\0';

printf("%s", result);

return 0;
}
Convert Camel Case in #include <stdio.h>
string
int main() {
char str[200], result[200];
int i = 0, j = 0, capitalize = 0;

fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {

if (str[i] == ' ' || str[i] == '\n') {


capitalize = 1;
}
else {
if (capitalize && str[i] >= 'a' && str[i] <= 'z') {
result[j] = str[i] - 32;
capitalize = 0;
}
else if (j == 0 && str[i] >= 'a' && str[i] <= 'z') {
result[j] = str[i];
}
else if (!capitalize && str[i] >= 'A' && str[i] <= 'Z') {
result[j] = str[i] + 32;
}
else {
result[j] = str[i];
}

j++;
}

i++;
}

Complexity IT Job Care Page 6


Regular Batch-08 (C Programming)

result[j] = '\0';

printf("%s", result);

return 0;
}
Sort characters in string #include <stdio.h>

int main() {
char str[100];
int i = 0, j = 0, len = 0;
char temp;

fgets(str, sizeof(str), stdin);


while (str[len] != '\0') {
if (str[len] == '\n') {
str[len] = '\0';
break;
}
len++;
}
for (i = 0; i < len - 1; i++) {
for (j = i + 1; j < len; j++) {
if (str[i] > str[j]) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}

printf("%s", str);

return 0;
}
Remove spaces from #include <stdio.h>
string
int main() {
char str[200], result[200];
int i = 0, j = 0;

fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {


if (str[i] != ' ' && str[i] != '\n') {
result[j] = str[i];

Complexity IT Job Care Page 7


Regular Batch-08 (C Programming)

j++;
}
i++;
}

result[j] = '\0';

printf("%s", result);

return 0;
}
Convert lowercase to #include <stdio.h>
uppercase
int main() {
char str[200];
int i = 0;

fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {


if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
i++;
}

printf("%s", str);

return 0;
}
Convert uppercase to #include <stdio.h>
lowercase
int main() {
char str[200];
int i = 0;

fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {


if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] + 32;
}
i++;
}

printf("%s", str);

Complexity IT Job Care Page 8


Regular Batch-08 (C Programming)

return 0;
}
Find largest word in #include <stdio.h>
sentence
int main() {
char str[200];
char word[50], largest[50];
int i = 0, j = 0, maxLen = 0, len = 0;

fgets(str, sizeof(str), stdin);

while (1) {
if (str[i] != ' ' && str[i] != '\0' && str[i] != '\n') {
word[j++] = str[i];
} else {
word[j] = '\0';

len = 0;
while (word[len] != '\0') len++;

if (len > maxLen) {


maxLen = len;

int k = 0;
while (word[k] != '\0') {
largest[k] = word[k];
k++;
}
largest[k] = '\0';
}

j = 0;
}

if (str[i] == '\0') break;


i++;
}

printf("%s", largest);

return 0;
}
Remove duplicate #include <stdio.h>
characters
int main() {

Complexity IT Job Care Page 9


Regular Batch-08 (C Programming)

char str[200], result[200];


int i = 0, j = 0, k = 0;
int found;

fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {

if (str[i] == '\n') {
i++;
continue;
}

found = 0;

for (k = 0; k < j; k++) {


if (result[k] == str[i]) {
found = 1;
break;
}
}

if (!found) {
result[j] = str[i];
j++;
}

i++;
}

result[j] = '\0';

printf("%s", result);

return 0;
}
Convert String to Title #include <stdio.h>
Case
int main() {
char str[200];
int i = 0;

fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {

Complexity IT Job Care Page 10


Regular Batch-08 (C Programming)

if (i == 0 || str[i - 1] == ' ') {

if (str[i] >= 'a' && str[i] <= 'z')


str[i] = str[i] - 32;
}
else {

if (str[i] >= 'A' && str[i] <= 'Z')


str[i] = str[i] + 32;
}

i++;
}

printf("%s", str);

return 0;
}
Count words in sentence #include <stdio.h>

int main() {
char str[200];
int i = 0, count = 0;

fgets(str, sizeof(str), stdin);

while (str[i] != '\0') {

if (str[i] != ' ' && (i == 0 || str[i - 1] == ' ')) {


count++;
}

i++;
}

printf("%d", count);

return 0;
}

Complexity IT Job Care Page 11


Regular Batch-08 (C Programming)

MCQ from String

1. Which header file is used for string functions?

A) stdio.h
B) string.h
C) conio.h
D) math.h
Answer: B

2. What is used to terminate a string in C?

A) \n
B) \t
C) \0
D) EOF
Answer: C

3. strlen("hello") returns:

A) 4
B) 5
C) 6
D) 0
Answer: B

4. strcmp("abc","abc") returns:

A) 1
B) -1
C) 0
D) error
Answer: C

5. Which function is used to copy a string?

A) strcat()
B) strcpy()
C) strcmp()
D) strlen()
Answer: B

6. strcat() is used for:

A) Compare
B) Copy

Complexity IT Job Care Page 12


Regular Batch-08 (C Programming)

C) Concatenate
D) Reverse
Answer: C

7. String in C is stored as:

A) Integer array
B) Character array
C) Float array
D) Double array
Answer: B

8. Which function compares two strings?

A) strcat
B) strcpy
C) strcmp
D) strlen
Answer: C

9. strlen() does NOT count:

A) Letters
B) Digits
C) Space
D) Null character
Answer: D

10. Which of these is correct string declaration?

A) char str;
B) char str[10];
C) string str;
D) char = str[10];
Answer: B

11. Output of strcmp("a","b") is:

A) 0
B) negative value
C) positive value
D) error
Answer: B

12. gets() function is:

Complexity IT Job Care Page 13


Regular Batch-08 (C Programming)

A) Safe
B) Recommended
C) Unsafe
D) Modern
Answer: C

13. Which function is unsafe?

A) fgets
B) gets
C) printf
D) scanf
Answer: B

14. String ends with:

A) space
B) null character
C) newline
D) tab
Answer: B

15. strrev() is:

A) Standard C function
B) Non-standard function
C) Math function
D) Input function
Answer: B

16. Which loop is best for string traversal?

A) switch
B) if
C) while
D) goto
Answer: C

17. Index of string starts from:

A) 1
B) 0
C) -1
D) 10
Answer: B

Complexity IT Job Care Page 14


Regular Batch-08 (C Programming)

18. ASCII value of 'A' is:

A) 97
B) 65
C) 48
D) 0
Answer: B

19. Which function joins two strings?

A) strcpy
B) strcat
C) strcmp
D) strlen
Answer: B

20. fgets() is used for:

A) Output string
B) Input string with spaces
C) Compare strings
D) Delete string
Answer: B

Written from String

1. Find ASCII value of each character in a string.


2. Convert first letter of each word to uppercase.
3. Convert string into sentence case.
4. Check if two strings have same words.

Complexity IT Job Care Page 15

You might also like