FAF 233 Nicolai Petcov 10
FAF 233 Nicolai Petcov 10
Computer Programming
Laboratory work #4
Author: Verified:
Nicolai Petcov Alexandru Furdui
std. gr. FAF-233
Theory Background
Strings and characters are fundamental elements in computer programming, often
used to represent and manipulate text-based data. While a single character can be seen
as the smallest unit of text, a string is a sequence of characters, forming a textual struc-
ture.
In programming, characters are typically represented using characters from the ASCII or
Unicode character sets, where each character is associated with a numeric code. These
codes allow computers to understand and process text-based information.
Strings are collections of characters arranged in a linear, sequential fashion. Each char-
acter within a string has a specific position or index, starting from the first character
at index 0 and increasing incrementally. This indexing system enables easy access to
individual characters within the string.
Easy3:Implement a function that reads a string and counts the number of vowels (a,
e, i, o, u) in it.
Medium1:Implement a function that reads a sentence from the user and counts the num-
ber of punctuation marks (e.g., periods, commas, question marks) in the sentence.
Technical implementation
Pseudocode.
Include the library " stdio . h "
Include the library " stdlib . h "
Include the library " string . h "
Function easy1 (s , n ) :
Declare variable i
Declare variable k
For i from 0 to ( n / 2) :
If s [ i ] is equal to s [ n - 1] , then :
Set k to 1
Else :
Set k to 0
Decrease n by 1
If k is equal to 1 , then :
Print " It ’s ␣ a ␣ palindrome "
Else :
Print " It ␣ isn ’t ␣ a ␣ palindrome "
Function easy2 (s , n ) :
Declare variable i
Declare an array up_s of size 100
Declare variable k
Copy the string s to up_s
For i from 0 to n :
If up_s [ i ] is not equal to a space , then :
Set k to the ASCII code of up_s [ i ]
Subtract 32 from k
Set up_s [ i ] to the character with code k
Print " The ␣ word ␣ with ␣ uppercases : ␣ " and up_s
Function easy3 (s , n ) :
Declare variable k
Declare variable i and set it to 0
Set k to 0
For i from 0 to n :
If s [ i ] is equal to ’a ’ or ’e ’ or ’i ’ or ’o ’ or ’u ’
FAF-233 Nicolai Petcov; Laboratory Work №2 4
or
’A ’ or ’E ’ or ’I ’ or ’O ’ or ’U ’ , then :
Increase k by 1
Print " The ␣ number ␣ of ␣ vowels : ␣ " and k
Function medium1 ( s ) :
Declare variable i
Declare variable k and set it to 0
Declare variable n and set it to the length of string s -
1
For i from 0 to n :
If s [ i ] is equal to ’ , ’ or ’. ’ or ’? ’ or ’! ’ or ’ - ’
or ’: ’ or
’; ’ or ’[ ’ or ’] ’ or ’{ ’ or ’} ’ or ’( ’ or ’) ’ or
’\ ’ ’ or ’" ’ , then :
If s [ i ] is equal to ’. ’ and s [ i +1] is equal to ’.
’ and s [ i +2] is equal to ’. ’ , then :
Increase k by 1
Increase i by 3
Else :
Increase k by 1
Print " The ␣ number ␣ of ␣ the ␣ punctuation ␣ marks : ␣ " and k
Function basetask ( s ) :
Declare variable i
Declare an array smallest of size 1000
Declare variable wordlength and set it to 1000
Declare an array currentWord of size 1000
Declare variable currentLength and set it to 0
Declare variable n and set it to the length of string s
For i from 0 to n :
Declare variable currentChar and set it to s [ i ]
If currentChar is a letter ( from ’a ’ to ’z ’ or from ’
A ’ to ’Z ’) , then :
Add currentChar to currentWord
Increase currentLength by 1
Else :
If currentLength is greater than 0 and
FAF-233 Nicolai Petcov; Laboratory Work №2 5
Main function :
Declare an array s of size 100
Declare an array med1 of size 100
Declare an array basestring of size 100
Print " Enter ␣ a ␣ word ␣ for ␣ easy ␣ tasks , ␣ or ␣ a ␣ sentence : ␣ "
Read a string into s
Set n to the length of string s - 1
Call the function easy1 (s , n )
Print an empty line
Call the function easy2 (s , n )
Print an empty line
Call the function easy3 ( s , n )
Print an empty line
Print " Enter ␣ a ␣ sentence ␣ to ␣ count ␣ the ␣ number ␣ of ␣
punctuation ␣ marks ␣ in ␣ it : ␣ "
Read a string into med1
Call the function medium1 ( med1 )
Print " Enter ␣ a ␣ sentence ␣ for ␣ basetask : ␣ "
Read a string into basestring
Call the function basetask ( basestring )
Return 0
https://github.com/Nicolashaaa/LAB4.git
FAF-233 Nicolai Petcov; Laboratory Work №2 6
Bibliography
1. https://www.programiz.com/c-programming/c-strings
2. https://www.scaler.com/topics/how-to-print-string-in-c/
3. https://github.com/
4. https://linuxhint.com/convert-string-to-boolean-value-cpp/
5. https://www.javatpoint.com/ascii-value-in-c
6. https://www.programiz.com/c-programming/library-function/string.h/strcpy
7. https://stackoverflow.com/questions/12784766/check-substring-exists-in-a-string-in-
c
8. https://www.codingame.com/playgrounds/14213/how-to-play-with-strings-in-c
9. https://www.studytonight.com/c/string-and-character-array.php
Results
1. Strings in C are represented as arrays of characters, and they can be declared and
initialized using the array syntax.
2. The ’strlen’ function from the ¡string.h¿ library can be used to determine the length
of a string.
FAF-233 Nicolai Petcov; Laboratory Work №2 7
4. Character input and output can be accomplished through the standard input and
output functions, such as ’printf’ and ’scanf’.
6. String comparison is possible with the ’strcmp’ function, which returns 0 when two
strings are equal.
8. Guard against buffer overflows by allocating adequate memory and validating user
inputs.
Conclusion
Working with strings and character elements is a crucial part of C programming.
Understanding string manipulation, character input/output, and common issues is essen-
tial for writing robust and reliable C programs. By following best practices and using
standard library functions, developers can effectively work with strings and character
elements while minimizing potential errors.