top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a program that takes input as string and return as integer ?

+2 votes
399 views
Write a program that takes input as string and return as integer ?
posted Jun 11, 2015 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

You can use "atoi()" predefined function for it.
Or you can write your own atoi() function.

    #include<stdio.h>
    int my_atoi(char *);
    main()
    {
        char s[100];
        int res;
        printf("Enter a string\n");
        scanf("%[^\n]",s);
        res=my_atoi(s);
        printf("Integer value is  = %d\n",res);
    }
    int my_atoi(char *p)
    {
        int num=0,i;
        for(i=0;p[i];i++)
            num = num*10 + p[i]-'0';
        return num;

    }
answer Jun 12, 2015 by Chirag Gangdev
Similar Questions
+2 votes

Weight of vowels that appear in the string should be ignored and All non-alphabetic characters in the string should be ignored.

Weight of each letter is its position in the English alphabet system, i.e. weight of a=1, weight of b=2, weight of c=3, weight of d=4, and so on….weight of y=25, weight of z=26
Weight of Upper-Case and Lower-Case letters should be taken as the same, i.e. weight of A=a=1, weight of B=b=2, weight of C=c=3, and so on…weight of Z=z=26.

Example1:
Let us assume the word is “Hello World!!”
Weight of “Hello World!!” = 8+0+12+12+0+0+23+0+18+12+4+0+0 = 89
Note that weight of vowels is ignored. Also note that the weight of non-alphabetic characters such as space character and ! is taken as zero.

+3 votes

Given input as a string, return an integer as the sum of all numbers found in the string

Input: xyzonexyztwothreeeabrminusseven
Output : 1 + 23 + (-7) = 17

...