Strings

1. Write a program that reads a string of maximum 30 characters from the user and prints it in uppercase. Note that non-alphabet characters should be printed as is.

#include <stdio.h>
int main (int argc, char * argv[]) {
    char str[31] = { '\0' };

    printf("Enter a string (up to 30 characters) : ");
    scanf("%30[^\n]s",str);

    for(int i=0; i<31 && str[i]; i++) {
        if(str[i]>='a' && str[i]<='z') {
            printf("%c",str[i]-'a'+'A');
        } else {
            printf("%c",str[i]);
        }
    }
}

 

2. What shall be the output of above program for input “It’s a great time to become a good programmer”.


#include <stdio.h>

#include <string.h>

int main (int argc, char * argv[]) {
    char message[256] = { 0 };

    printf("Enter message of the day: ");
    scanf("%255[^\n]s",message);
    printf("Your message is %lu characters long\n", strlen(message));
    return 0;
}

Ans: Your message is 47 characters long

 

3. Write a program that reads a string of maximum 30 characters from the user and prints the number of small case alphabets in the string entered by the user.

#include <stdio.h>

int main (int argc, char * argv[]) {
    char str[31] = { '\0' };
    int count=0;

    printf("Enter a string (up to 30 characters) : ");
    scanf("%30[^\n]s",str);

    for(int i=0; i<31 && str[i]; i++) {
        if(str[i]>='a' && str[i]<='z') {
            count++;
        }
    }
    printf("%s has %d small case alphabets\n", str, count);
}

 

4. Write a program that reads a string of maximum 30 characters from the user and prints the number of non-alphabet characters in the string entered by the user.

#include <stdio.h>

int main (int argc, char * argv[]) {
    char str[31] = { '\0' };
    int count=0;

    printf("Enter a string (up to 30 characters) : ");
    scanf("%30[^\n]s",str);

    for(int i=0; i<31 && str[i]; i++) {
        if(!((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))) {
            count++;
        }
    }
    printf("%s has %d non-alphabet characters\n", str, count);
}

 

5. Draw a flowchart for implementation of strcmp function.

 

 

 

6. Add NULL error checks to below implementation.

01: int my_strcmp (const char * s1, const char * s2) {
02:     while ( (*s1 != '\0') && (*s1 == *s2) ) {
03:         s1++;
04:         s2++;
05:     }
06:     return (*s1-*s2);
07: }

 

int my_strcmp (const char * s1, const char * s2) {
    if(s1 == NULL && s2!= NULL) {
        return -*s2;
    }
    if(s2 == NULL && s1!= NULL) {
        return *s1;
    }
    if(s1 == NULL && s2 == NULL) {
        return 0;
    }
    while ( (*s1 != '\0') && (*s1 == *s2) ) {
        s1++;
        s2++;
    }
    return (*s1-*s2);
}

 

7. Trace control flow of above function for arguments

1. “C” and “Champion”

Ans:

  1. Control reaches line 02, s1 is pointing to “C” and s2 is pointing to “Champion”.
  2. Control condition evaluates to true, *s1 is not NULL and first character of both strings match.
  3. Control advances to line 03. s1 is incremented is now pointing to end of string.
  4. Control advances to line 04. s2 is incremented is now pointing to “hampion”.
  5. End of loop is reached, control jumps to line 02.
  6. Control condition evaluates to false. Control jumps outside loop, line 06.
  7. Value ‘\0’ – ‘h’ is returned.

 

2. “Top programmer” and “Top programmer”

Ans:

  1. Control reaches line 02, s1 is pointing to “Top programmer” and s2 is pointing to “Top programmer”.
  2. Control condition evaluates to true, *s1 is not NULL and first character of both strings match.
  3. Control advances to line 03. s1 is incremented is now pointing to “op programmer”.
  4. Control advances to line 04. s2 is incremented is now pointing to “op programmer”.
  5. End of loop is reached, control jumps to line 02.
  6. Steps 2-5 are repeated for each character of both strings until both s1 and s2 are pointing to respective end of strings.
  7. Control jumps to line 02.
  8. Control condition evaluates to false. Control jumps outside loop, line 06.
  9. Value ‘\0’ – ‘\0’ is returned.

 

8. Draw a flowchart for function strncmp.

 

 

9. Implement function strncmp.

int my_strncmp (const char * s1, const char * s2, unsigned int n) {
    int count = 0;
    while ( (*s1 != '\0') && (*s1 == *s2) && count != n-1) {
        s1++;
        s2++;
        count++;
    }
    return (*s1-*s2);
}

 

10. Implement strcpy using while loop.

char * my_strcpy (char *dest, const char *src) {
    int i = 0;

    /*Loop terminates when end of src string is reached*/
    while('\0' != src[i]) {
        dest[i] = src[i];
        i++;
    }
    dest[i] = '\0'; /*Terminate dest with null byte*/
    return dest;
}

 

11. Draw a flowchart for function strncpy.

 

 

12. Implement function strncpy.

char * my_strncpy (char *dest, const char *src, unsigned int n) {
    int i = 0;
    for(;i<n && src[i]; i++) {
        dest[i] = src[i];
    }
    return dest;
}