Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. (a) Write a program that reverses strings (Jon Campbell from Microsoft indica

ID: 3681928 • Letter: 1

Question

1. (a) Write a program that reverses strings (Jon Campbell from Microsoft indicated this could be a possible interview question). You need to define a function called string_reverse () that accepts a string as an argument and reverses the string without creating an extra array to perform the reversing, i.e. "CptS 121 is cool!" becomes "!looc si 121 StpC". This is a form of in place reversal. The function should return a pointer to the reversed string. Implement a solution to this function using array notation only. NOTE: you may NOT use any <string.h> library functions to solve this problem!

(b) Rewrite the string_reverse() function using pointer notation and pointer arithmetic only.

2. Write a program that tests the following functions. Yes, you must implement the below functions without using any <string.h> library functions. You may use array and/or pointer notation to define the functions.

3. Recall that bubble sort is a technique for sorting an array. A bubble sort compares adjacent array elements and exchanges their values if they are out of order. In this way, the smaller values "bubble" to the top of the array (toward element 0), while the larger values sink to the bottom of the array. After the first pass of a bubble sort, the last array element is in the correct position; after the second pass the last two elements are correct, and so on. Thus, after each pass, the unsorted portion of the array contains one less element. Write and test a function that implements this sorting method on strings! You MAY use <string.h> library functions to help with sorting the strings. NOTE: you will need to run your sort on an array of strings. Any array of strings is viewed as a 2-dimensional array of characters in which each row is terminated by the null character. Thus, each row represents one string in the array of strings. Consider the below information.

char array_strings[10][50]; /* Declare an array of strings which contains at most 10 strings (the number of rows) which have a length of at most 50 characters (the number of columns) */

/* To copy a string into one row of the 2-D array use string copy as follows. */

strcpy (array_strings[0], "a string"); /* Note: supplying one dimension as an index into a 2-D array supplies a starting address for a row! */

strcpy (array_strings[1], "cat");

/* Logical view */

0

1

2

3

4

5

6

7

8

...

49

0

'a'

'  '

's'

't'

'r'

'i'

'n'

'g'

''

...

?

1

'c'

'a'

't'

''

?

?

?

?

?

...

?

...

...

...

...

...

...

...

...

...

...

...

...

9

?

?

?

?

?

?

?

?

?

...

?

0

1

2

3

4

5

6

7

8

...

49

0

'a'

'  '

's'

't'

'r'

'i'

'n'

'g'

''

...

?

1

'c'

'a'

't'

''

?

?

?

?

?

...

?

...

...

...

...

...

...

...

...

...

...

...

...

9

?

?

?

?

?

?

?

?

?

...

?

Explanation / Answer

1.a )WRITE A C PROGRAM TO REVERSE A STRING WITHOUT USING STRING FUNCTION :-

------------------------------------------------------------------------------------------------------------------------------------

output :

1.b )WRITE A C PROGRAM TO REVERSE A STRING USING POINTER :

----------------------------------------------------------------------------------------------------

2)String copy without using strcpy in c programming language :-

-----------------------------------------------------------------------------------------------

#include<stdio.h>

void stringCopy(char[],char[]);

int main(){

    char str1[100],str2[100];

    printf("Enter any string: ");

    scanf("%s",str1);

    stringCopy(str1,str2);

    printf("After copying: %s",str2);

    return 0;

}

void stringCopy(char str1[],char str2[]){

    int i=0;

    while(str1[i]!=''){

         str2[i] = str1[i];

         i++;

    }

    str2[i]='';

}

Sample output:

Enter any string: cquestionbank.blogspot.com

After copying: cquestionbank.blogspot.com

String concatenation in c without using string functions :-

---------------------------------------------------------------------------------------

#include<stdio.h>

void stringConcat(char[],char[]);
int main(){

    char str1[100],str2[100];
    int compare;

    printf("Enter first string: ");
    scanf("%s",str1);

    printf("Enter second string: ");
    scanf("%s",str2);

    stringConcat(str1,str2);

    printf("String after concatenation: %s",str1);

    return 0;
}

void stringConcat(char str1[],char str2[]){
    int i=0,j=0;
   
   
    while(str1[i]!=''){
         i++;
    }

    while(str2[j]!=''){
         str1[i] = str2[j];   
         i++;
         j++;
    }

    str1[i] = '';
}

Sample output:
Enter first string: cquestionbank
Enter second string: @blogspot.com
String after concatenation: cquestionbank@blogspot.com

C program to compare two strings without using string functions :-

---------------------------------------------------------------------------------------------------------

#include<stdio.h>

int stringCompare(char[],char[]);

int main(){

    char str1[100],str2[100];

    int compare;

    printf("Enter first string: ");

    scanf("%s",str1);

    printf("Enter second string: ");

    scanf("%s",str2);

    compare = stringCompare(str1,str2);

    if(compare == 1)

         printf("Both strings are equal.");

    else

         printf("Both strings are not equal");

    return 0;

}

int stringCompare(char str1[],char str2[]){

    int i=0,flag=0;

   

    while(str1[i]!='' && str2[i]!=''){

         if(str1[i]!=str2[i]){

             flag=1;

             break;

         }

         i++;

    }

    if (flag==0 && str1[i]=='' && str2[i]=='')

         return 1;

    else

         return 0;

}

Sample output:

Enter first string: cquestionbank.blogspot.com

Enter second string: cquestionbank.blogspot.com

Both strings are equal.

C Program to Find Length of String Without using Library Function :-

------------------------------------------------------------------------------------------------

#include<stdio.h>

int main() {

   char str[100];

   int length;

   printf(" Enter the String : ");

   gets(str);

   length = 0;  // Initial Length

   while (str[length] != '')

      length++;

   printf(" Length of the String is : %d", length);

   return(0);

}