Question

In: Computer Science

C Program: Write your own stringLength function (without calling a pre-written library function) that returns the...

C Program:

Write your own stringLength function (without calling a pre-written library function) that returns the length of the string argument in characters, not including the terminating ‘\0’ character.  

Write four versions of a void printString function (i.e., printString1, printString2, etc) each of which prints its string argument character-by-character but using the following techniques:
printString1: array indexing
printString2: pointer/offset with the array name as a pointer,
printString3: pointer indexing, and
printString4: pointer/offset with a pointer

Write a void function called swapChar that swaps the values stored in two char variables.

Write a void reverseString function that accepts a single string parameter and reverses the characters in the string using calls to your stringLength and swapChar functions.

Write a main function which:
1) declares an array of characters large enough to hold 20 characters plus the terminating null character,
2) prompts the user for an input string no longer than 20 characters,
3) calls each of the 4 printString functions passing the input string as the argument,
4) calls reverseString passing the input string as the argument,
5) calls each of the 4 printString functions passing the (reversed) input string as the argument.

Solutions

Expert Solution

#include<stdio.h>
//function that returns the length of the
//char array passed.
int stringLength(char str[])
{
   int len =0;
   while(str[len] !='\0')
   {
       len++;
   }
   return len;
}
//function that prints the
//char array passed by using array indexing.
void printString1(char str[])
{
   printf("\nPrinting String Type 1\n");
   int len = stringLength(str);
   int i;
   for(i =0;i<len;i++)
   {
       printf("%c",str[i]);
   }
   printf("\n");
}

//function that prints the
//char array passed by using pointer/offset with the array name as pointer.
void printString2(char str[])
{
   printf("\nPrinting String Type 2\n");
   int len = stringLength(str);
   int i;
   for(i =0;i<len;i++)
   {
       printf("%c",*(str + i));
   }
   printf("\n");
}

//function that prints the
//char array passed by using pointer indexing.
void printString3(char str[])
{
   printf("\nPrinting String Type 3\n");
   int len = stringLength(str);
   int i;
   for(i =0;i<len;i++)
   {
       printf("%c",i[str]);
      
   }
   printf("\n");
}

//function that prints the
//char array passed by using pointer/offset with a pointer..
void printString4(char str[])
{
   printf("\nPrinting String Type 4\n");
   int len = stringLength(str);
   int i;
   for(i =0;i<len;i++)
   {
       printf("%c",*(i+str));
   }
   printf("\n");
}
//function that takes address of two chars
//and swaps them.
void swapChar(char *one,char *two)
{
   char c = *one;
   *one = *two;
   *two = c;
}

//function that reverses the given char array.
void reverseString(char str[])
{
   int len = stringLength(str);
   int i =0;
   //swap the i_th char and len-i-1 char
   //each time till i<len/2;
   for(i =0;i<=len/2;i++)
   {
       swapChar(&str[i],&str[len-i-1]);
   }
}

int main()
{
   char str[21];
   //prompt user for input and store it.
   printf("Enter string: ");
   gets(str);
  
   if(stringLength(str)>20)
   {
       printf("Input string must be less than 20 chars");
       return 0;
   }
   //call print versions
   printString1(str);
   printString2(str);
   printString3(str);
   printString4(str);
  
   //reverse the string
   reverseString(str);
   printString1(str);
   printString2(str);
   printString3(str);
   printString4(str);
   return 0;
}


Related Solutions

in c++, without using any built-in random library already exist, instead,write your own function to generate...
in c++, without using any built-in random library already exist, instead,write your own function to generate random char from a list of: 10 letters T 10 letters H 10 letters A 10 letters N 10 letters K
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
Method calling in c# I need to write methods for calling the min and max numbers...
Method calling in c# I need to write methods for calling the min and max numbers using this code; Console.WriteLine("Calling highest method."); Console.WriteLine("Highest number is: {0}", highest(3)); Console.WriteLine("Calling lowest method."); Console.WriteLine("Lowest number is: {0}", lowest(3));
Write a C/C++ program to maintain a fixed size library of 1024 documents, whose size randomly...
Write a C/C++ program to maintain a fixed size library of 1024 documents, whose size randomly ranges between 2MB to 3MB.
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer. example: input: 7 1 2 3 4 5 6 7 output: 1 3 5 7 2 4 6 8
Write a method that returns the result when the calling object is multiplied by a scalar...
Write a method that returns the result when the calling object is multiplied by a scalar value. For example, the PolyTerm 2.4x^3 multiplied by -1.2 should return the PolyTerm object representing -2.88x^3. Language: Java. Method name be like: scalarMultiply(double) Some Outputs: Test 1: Coefficient =1, Exponent = 1 scalarMultiply(1.2).coefficient return 1.2; scalarMultiply(1.2).exponent returns 1. Test 2: Coefficient =2.4, Exponent = 3 scalarMultiply(-1.2).coefficient returns -2.88 scalarMultiply(-1.2).exponent return 3 Test 3: Coefficient =-1.5 Exponent = 0 scalarMultiply(0).coefficient returns 0 scalarMultiply(0).exponent returns 3...
The goal of this project is to practice (Write a C Program) with a function that...
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is: void func ( float (*f)(float*, int), float* a, int length); This means the function: func has three parameters: float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT