C Program to Print the Square of all the Numbers in a String

This is a C Program to input a string with atleast one number, print the square of all the numbers in a string.

Problem Description

This C Program takes an input string with atleast one number and prints the square of all the numbers in the string.

Problem Solution

Take input from the user and perform string operations as shown in the program below.

Program/Source Code

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;
}
Program Explanation

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.

advertisement
advertisement

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.

Note: Join free Sanfoundry classes at Telegram or Youtube

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.

advertisement
Runtime Test Cases
 
$ 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

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.