[Legacy Content] This page contains very old information, which might not be valid anymore, they are only kept here for legacy purposes.
if you have any inquiries, feel free to contact me! Click here

Strings - Examples & Solutions

Created: Thursday, 05 December 2013 Written by Ehab Eldeeb

The Given Examples - as written in the footer of the sheet - are prepared by the computer department staff in AAST
I don't know whom exactly, But thank you for those ideas :)

Solutions are purely written by Ehab Eldeeb

Enjoy :)

 

Examples

Download the sheet from this link --> Download here

 

Solutions

Question 1:

I) "H" itself is a string, not a character .. to define the character H to be in position 2 in the string, do it like this --> a[2] = 'H';

II) in C, we don't have the datatype "string", but we have "array of characters", do it as follows:
char s1[10] = "Hello";
or char s1[10] = {'H', 'e', 'l', 'l', 'o', '\0'};

III) Arithmetic operations cannot be applied to strings.
if you would like to "merge" the two strings, use the function strcat

IV) Assignment operations cannot be applied to strings as well.
use the function strcpy instead

V) Many errors in this line:
- Pointer is of no use
- String Length is too short for the word "Hello World"
- writing "Hello World" is not like that, use this instead: char s[50] = "Hello World";

VI) as stated in part IV... But here we will use the strcmp function instead

 

Question 2:

1 is an integer

'1' is a character

"1" is a string

 

Question 3:

use strcpy(s1, NULL); // Correct me if I'm wrong (?)

or s1[0] = '\0';

 

Question 4:

a) fill the string with the word "AAST"

b) I think it would give compiling errors

c) I think it would give compiling errors

 

Question 5:

scanf reads one word, and is terminated by hitting "space"

gets reads the whole string until you hit "enter" or as much as the string length could handle (can include spaces)

 

Question 6:

No difference is clear in these two functions

The only difference is how you write the function.

printf("%s", s1);

puts(s1);

 

Question 7:

a) strlen

b) strcpy

c) strcat

d) strcmp

 

Question 8:

This function is supposed to copy string s2 into string s1

But what would actually happen, is that it would print string1 as it is.

The first for loop is terminated before doing anything, because there's a semicolon after it..

Also, you would need only one for loop, not two .. (or you can use a while loop as shown below)

int i = 0;
while(source[i] != '\0'){
    dest[i] = source[i];
    i++;
} dest[i] = '\0'; // Force the destination string to close

 

Question 9:

We have written this before

Refer to this post: http://www.ehabeldeeb.com/aast/programming/77-c-string-functions

 

Question 10:

int freq(char s1[], char k){
    int num = 0;
    for(int i = 0; i < strlen(s1); i++){
        if(s1[i] == k)
            num++;
    }
    return num;
}

 

Question 11:

Easy method using a 3rd string

void swap(char s1[], char s2[]){
    char s3[500];
    strcpy(s3, s1);
    strcpy(s1, s2);
    strcpy(s2, s3);
}

You can also set a temp char to swap char by char

 

Question 12:

There's a function the the library "stdlib.h" called atoi (ascii to integer)

simply: int x = atoi(TheString);

 

Question 13:

Using Bubble Sort (the method you normally use to sort an array)

for (int i = 0; i < strlen(x); i++){
        for (int j = 0; j < strlen(x); j++){
            if( x[j] > x[i]){
                temp = x[i];
                x[i]= x[j];
                x[j] = temp;
            }
        }
    }

 

Question 14:

Using the ASCII codes for the characters, we can find out whether the char is a digit, alphabet, or special character, or whatever..

Using the table below, (Dec Value), We can find out what the ASCII code for a specific char is

Consonants are some specific letters (Read this on Wikipedia)

Anyway here's the code..

char x[200];
    int vowels = 0, consonants = 0, digits = 0, spaces = 0;
    puts("Enter the string:");
    gets(x);
    for(int i = 0; i < strlen(x); i++){
        if(x[i] == ' ')
            spaces++;
        else if (x[i] >= 48 && x[i] <= 57)
            digits++;
        else if (x[i] == 'a' || x[i] == 'e' || x[i] == 'o' || x[i] == 'u' || x[i] == 'i' || x[i] == 'A' || x[i] == 'E' || x[i] == 'O' || x[i] == 'U' || x[i] == 'I')
            vowels++;
        else if (x[i] == '?' || x[i] == '?' || x[i] == '?' || x[i] == '?' || x[i] == '?') // Write the consonants like this
            consonants++;
    }
    printf(" Vowels: %d\n Consonants: %d\n Digits: %d\n Spaces: %d", vowels, consonants, digits, spaces);

 

Question 15:

Using the table given in question 14 .. We can notice that the ASCII difference between a capi