Answered # Looping through string

vegaseat 1,720 Discussion Starter np complete 8 Discussion Starter np complete 8 Gribouillis 1,313 Discussion Starter np complete 8 Featured Reply Gribouillis 1,313 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

Some help ...

prime numbers are only divisible by unity and themselves

integers less than 2 and even numbers other than 2 are not prime

0

Example :

Long String : cwl8yv0wxuo?8v#wxa#yte3m0vr#5tdbt$k?j7ovzg$@$62nk@6c0$s#bbsmu1gqu7na85u3zl9903#518ukv2awgk0u#attjehlbu?o5y#c5yw83b2n@kq8$nq4fgvf9pqj0ejwbj87a33$0nqb0h04@yf0y7i4qgoq?qeik80be3z4iwqrb?sbbsrwa58k#cd9yt8g?k04jb0opdkflngpepl1rvtlw1z8j9p#kw$mzv$2p4ep$ycl8r3u5$hvvfz1ny7vn2vq3a?hoje@czgqxghqccasm432ovlh8m9m010nt3zv#l2n@8?5h#jyw?f99l5e5e$7w87g8xfgc#$n7961xobpe315np1ht?dvqpe?h#@r81o823iyh1so35vhjmzc2x#3?pkjqh1sy5gc3sp$qvka5a2kcdpasfj@9b7dqvfwgucc5$xkaz3jxp4m#yu?s2j#hf9ul@tg?beeo@p4hn$wm#f0ev26b7k8g$zz4cps$z9qv95sxg5jwwnedu@yhq2ldfw#0#siesfrrzgo##zz52rfk$7ot97mxl#v9re$qgnoxfowgmg?q2lw3gtq7uxyo9ceosko58caw@82#awxvd3ezou#tzv?6sx9csy1w3h?d#z7kido

Answer : dxmzwxyvp@w$xyb$zufn111435

My code in C :

```
# include <stdio.h>
# include <math.h>
int prime_check ( int i )
{
int j = 0;
for(j=2; j <= sqrt(i); j++ )
if(!(i%j)) break;
if (j>sqrt(i))
return 1 ;
else
return 0 ;
}
int main (void)
{
int n, i, j = 0, prime = 0, composite = 0;
char arr[50], num[20] ;
char str[] = {That HUGE LONG string} ;
for ( i = 0; str[i] != '\0'; i++ )
{
if ( isdigit(str[i]) )
{
n = str[i] - '0' ;
if ( n != 1 && n != 0)
{
if ( prime_check (n) == 1 )
prime += n ;
else
composite += n ;
}
}
else
if ( (i < 25) && (isdigit (str[i]) == 0) )
arr[j++] = str[i] + 1 ;
}
itoa ( prime*composite, num, 10 ) ;
strcat ( arr, num ) ;
printf ("%s", arr ) ;
return 0;
}
```

I'm new in python, so I'm not being able to write an equivalent code for this.

0

I was trying to do that`arr[j++] = str[i] + 1 ;`

part. My python code was `arr+str(int(i)+1))`

. It says invalid literal for int() base 10. What should I do ? My objective is to take the each char from string ( upto first 25 chars ) and give them unit positive shift.

0

Here is my version for python 2

```
#!/usr/bin/env python
#-*-coding: utf8-*-
def prime_check(n):
'''
check if integer n is a prime, return True or False
'''
# 2 is the only even prime
if n == 2:
return True
# integers less than 2 and even numbers other than 2 are not prime
elif n < 2 or not n & 1:
return False
# loop looks at odd numbers 3, 5, 7, ... to sqrt(n)
for i in range(3, int(n**0.5)+1, 2):
if n % i == 0:
return False
return True
def main():
prime = 0
composite = 0;
from array import array
arr = ''
src = array('b', (
'cwl8yv0wxuo?8v#wxa#yte3m0vr#5tdbt$k?j'
'7ovzg$@$62nk@6c0$s#bbsmu1gqu7na85u3zl'
'9903#518ukv2awgk0u#attjehlbu?o5y#c5yw'
'83b2n@kq8$nq4fgvf9pqj0ejwbj87a33$0nqb'
'0h04@yf0y7i4qgoq?qeik80be3z4iwqrb?sbb'
'srwa58k#cd9yt8g?k04jb0opdkflngpepl1rv'
'tlw1z8j9p#kw$mzv$2p4ep$ycl8r3u5$hvvfz'
'1ny7vn2vq3a?hoje@czgqxghqccasm432ovlh'
'8m9m010nt3zv#l2n@8?5h#jyw?f99l5e5e$7w'
'87g8xfgc#$n7961xobpe315np1ht?dvqpe?h#'
'@r81o823iyh1so35vhjmzc2x#3?pkjqh1sy5g'
'c3sp$qvka5a2kcdpasfj@9b7dqvfwgucc5$xk'
'az3jxp4m#yu?s2j#hf9ul@tg?beeo@p4hn$wm'
'#f0ev26b7k8g$zz4cps$z9qv95sxg5jwwnedu'
'@yhq2ldfw#0#siesfrrzgo##zz52rfk$7ot97'
'mxl#v9re$qgnoxfowgmg?q2lw3gtq7uxyo9ce'
'osko58caw@82#awxvd3ezou#tzv?6sx9csy1w'
'3h?d#z7kido'
))
ord_zero = ord('0')
def isdigit(n):
return 0 <= n - ord_zero < 10
for i, x in enumerate(src):
if isdigit(x):
n = x - ord_zero
if n > 1:
if prime_check(n): # if n in (2, 3, 5, 7):
prime += n
else:
composite += n
elif i < 25:
arr += chr(x + 1)
arr += str(prime * composite)
print(arr)
if __name__ == "__main__":
main()
""" my output -->
dxmzwxyvp@w$xyb$zufn111435
"""
```

Instead of a byte string, we use an array of small integers from module array (in python 3, we'd use the byte type, which is a read-only array of small integers). For prime_check(), we use Vegaseat's code

*Edited 3 Years Ago by Gribouillis*

0

I have error in this line `ans += str(i + 1)`

. It says Can't convert 'int' object to str implicitly. I declared `i = ""`

, before this.

1

Why did you declare i = "" ? Python cannot add a str to an int.

*Edited 3 Years Ago by Gribouillis*

This question has already been answered. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...