This is a C Program to input a string with atleast one number, print the square of all the numbers in a string.
This C Program takes an input string with atleast one number and prints the square of all the numbers in the string.
Take input from the user and perform string operations as shown in the program below.
Here is a source code of the C program to print the squares of all the numbers entered in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Input a String with atleast one Number, Print * the Square of all the Numbers in a String */ #include <stdio.h> #include <string.h> #include <ctype.h> #include <math.h> struct detail { int number; int square; }; int update(struct detail [], int, int); int toint(char []); int main() { struct detail s[10]; char unit[20], string[100]; char c; int num, i, j = 0, count = 0; printf("Enter string: "); i = 0; do { fflush(stdin); c = getchar(); string[i++] = c; } while (c != '\n'); string[i - 1] = '\0'; printf("The string entered is: %s\n", string); for (i = 0; i < strlen(string); i++) { while (i < strlen(string) && !isspace(string[i])) { unit[j++] = string[i++]; } if (j != 0) { unit[j] = '\0'; num = toint(unit); count = update(s, num, count); j = 0; } } printf("*****************\nNumber\tSquare\n*****************\n"); for (i = 0; i < count; i++) { printf("%d\t %d\n", s[i].number, s[i].square); } return 0; } int update(struct detail s[], int num, int count) { s[count].number = num; s[count].square = num * num; return (count + 1); } int toint(char str[]) { int len = strlen(str); int i, num = 0; for (i = 0; i < len; i++) { num = num + ((str[len - (i + 1)] - '0') * pow(10, i)); } return num; }
This C program, we are reading the string using ‘string[]’ array variable. Do while loop is used to accept the input value of the string using getchar() function and assign the value to ‘c’ character variable.
While loop is used to check the value of character variable ‘c’ is not equal to empty character null. If the condition is true, then execute the iteration of the loop. For loop is used to compute the square of all the numbers.
While loop is used to check the value of ‘i’ variable is less than the length of the string and string should not consist any empty space using isspace() function using logical AND operator. If the condition is true then assign the value of ‘string[]’ array variable to ‘unit[]’ variable.
If condition statement is used to check the value of ‘j’ variable is not equal to 0. If the condition is true, then execute the statement. Assign the value of ‘unit[]’ array variable as null. The toint() function is used to compute the square of all the numbers in a string.
For loop is used to compute the square of all the numbers in a string. Initialize the value of ‘i’ variable to 0 and check the value of ‘i’ variable is less than the value of ‘len’ variable. If the condition is true, then execute the loop.
Compute the power value of 10 to the power of value of ‘i’ variable. Multiply the length of the string with the power value. Add the resulted value with the ‘num’ variable and return the value.
In update() function compute the square of the num variable value and assigning to the structure variable s[count].square’. Print the square of all the numbers in a string using printf statement.
$ gcc numbersquare.c -lm $ ./a.out Enter string: 1 2 3 4 5 The string entered is: 1 2 3 4 5 ***************** Number Square ***************** 1 1 2 4 3 9 4 16 5 25
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check C Books