nAutAxH AhmAd nAutAxH AhmAd - 18 days ago 7
C Question

Strings addition in C Program

How to add an integer to the ASCII value of the characters in a string?

Example:- if the ASCII values of the characters in string

"eat"
are 101 97 116, how would I add an integer value, suppose it is 10, to each ASCII value so it changes to 111 107 126.

Can anyone write code in C for that??

Answer

You'd have to loop over each element of the char array and add 10 one by one.

int i;
char string[4] = "abc\0";
for(i = 0; i < string[i]; i++) {
    string[i] += 10;
}

Since a char is an (n bit) integer you can simply add integers to it.