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.
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
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
C ++ program that uses a function “IsDivisibleBy5” that returns a 1 if the input integer...
C ++ program that uses a function “IsDivisibleBy5” that returns a 1 if the input integer is evenly divisible by 5. Return 0 otherwise. Also include a function “IsOdd()” that returns a 1 if the input integer is odd. Return 0 otherwise.
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 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...
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT