I have write code and it producing correct output but it can not fill all requirements please optimize my code or give me some suggestion.
It is my code...
// it is my code it produces correct output but not complete requirement.
public String flipCoins(int N,String s) {
char ch[] = new char[N];
for(int i=0;i<N;i++){
ch[i] = s.charAt(i);
}
for(int i=1;i<N;i++){
if(ch[i] == '1' || ch[i-1] == '1'){
ch[i] = '1';
ch[i-1] = '1';
}else if(ch[i] == '0' || ch[i-1] == '0'){
ch[i] = '1';
ch[i-1] = '1';
}else if(ch[i] == '0' || ch[i-1] == '1'){
ch[i] = '1';
ch[i-1] = '0';
}else if(ch[i] == '1' || ch[i-1] == '0'){
ch[i] = '0';
ch[i-1]='1';
}
}
int count = 0;
for(char c: ch){
if(c == '1')
count++;
}
if(count == N)
return "Yes";
else
return "No";
}
What I learned from the question is that you can flip any number of time, so
the question boils down to find whether there are even nos. of zeroes or not beacuse you can swap 00 pair to make 11 else it would always be 10 or 01
i'm a beginner to programming and I'm running into a problem. I think that this is quite basic but I have looked around everywhere and can't find a solution (probably because of my own lack of understanding). This is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char inputString[999];
char inputArray[99][99];
int a = 0;
int b = 0;
fgets(inputString, sizeof inputString, stdin);
for(int i = 0; i <= (strlen(inputString)); i++) {
if(inputString[i] == ' ' || inputString[i] == '\0') {
inputArray[a][b] = '\0';
a++;
b = 0;
}
else {
inputArray[a][b] = inputString[i];
b++;
}
}
for (int i = 0; i < 99; i++) {
if (inputArray[i][0] == '\0') {
break;
}
for (int j = 0; j < 99; j++) {
iif (inputArray[i][0] == 'a' || inputArray[i][0] == 'e' || inputArray[i][0] == 'i' || inputArray[i][0] == 'o' || inputArray[i][0] == 'u' ||
inputArray[i][0] == 'A' || inputArray[i][0] == 'E' || inputArray[i][0] == 'I' || inputArray[i][0] == 'O' || inputArray[i][0] == 'U') {
if (inputArray[i][j] == '.') {
inputArray[i][j] = '\0';
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = '.';
inputArray[i][j + 4] = '\0';
i++;
j = 0;
}
}
else if (inputArray[i][j] == ',') {
inputArray[i][j] = '\0';
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = ',';
inputArray[i][j + 4] = '\0';
i++;
j = 0;
}
}
else {
if (inputArray[i][j] == '\0') {
inputArray[i][j] = 'm';
inputArray[i][j + 1] = 'o';
inputArray[i][j + 2] = 'o';
inputArray[i][j + 3] = '\0';
i++;
j = 0;
}
}
}
else if (inputArray[i][0] != 'a' || inputArray[i][0] != 'e' || inputArray[i][0] != 'i' || inputArray[i][0] != 'o' || inputArray[i][0] != 'u' ||
inputArray[i][0] != 'A' || inputArray[i][0] != 'E' || inputArray[i][0] != 'I' || inputArray[i][0] != 'O' || inputArray[i][0] != 'U' || inputArray[i][0] != '\0') {
printf("a");
i++;
j = 0;
}
}
}
for (int i = 0; i < 99; i++) {
printf("%s ", inputArray[i]);
if (inputArray[i][0] == '\0') {
break;
}
}
return 0;
}
Essentially I'm trying to input a string, separate them into words using a 2-D array, and then check if the first letters of each string in that array is a consonant or a vowel.
I'm using printf("a"); to test if my code works, but when i run the program and input something like "yo", this is the output:
yo
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayo
The letter "a" is printed 99 times, which is as many times as the inner for loop is ran, so i'm guessing it has something to do with the fact that the first element of every string in the array is also not a vowel and maybe it's a null character, so I tried adding if (inputArray[i][0] == '\0') { (like in the code), but it still doesn't work.
I'd really appreciate if you guys can help me out
EDIT: I changed the code a bit to make it more readable (thanks Benjamin Maurer!), but essentially what I'm trying to do in the first if statement is add "moo" to the end of a word if it starts with a vowel.
Your code starts normal, but why did you then switch from array notation to this absolute madness in the second loop with pointer dereferences? I'm not even going to try to understand that code, bc. it's unreadable.
According to your description, each of inputArray[i] is a string. So checking if inputArray[i][0] is a consontant/vowel should suffice, no?
#include <ctype.h>
#include <stdio.h>
// ...Read input...
for (int i = 0; i < 99; ++i) {
if (isalpha(inputArray[i][0])) {
char c = toupper(inputArray[i][0]);
switch (c) {
// Intentional fallthroughs
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
puts("is vowel!");
break;
default:
puts("is consonant");
}
}
}
Both isalpha and toupper are from ctype.h.
islapha checks whether the argument is an alphabetic latin character (a letter).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an array formed from a text file imported by stdin.
The text file looks like this:
"Name"
"Number"
"Name"
"Number"
...
The entire code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char** argv)
{
//number of arguments
if (argc > 2)
{
fprintf(stderr, "Too many arguments\n");
return 1;
}
//check argument 1
{
if (argc == 2)
{
unsigned i = 0;
while (i < strlen(argv[1]))
{
if ((isdigit(argv[1][i])) == 0)
{
fprintf(stderr, "Enter a number\n");
return 1;
}
i++;
}
}
else
{
fprintf(stderr, "argument\n");
return -1;
}
}
//find \n and separate
int g = 0;
int c = 0;
char buffer[102];
char people[42][102];
char numbers[42][102];
while (fgets(buffer, sizeof buffer, stdin) != NULL)
{
if (g % 2 == 0)
{
strcpy(people[c], buffer);
//printf("%s", people[c]);
}
if (g % 2 == 1)
{
strcpy(numbers[c], buffer);
c++;
}
g++;
}
//convert and remove \n
char conv_people[42][102];
for (int i = 0; i < c; i++)
{
for (unsigned j = 0; j < strlen(people[i]); j++)
{
if (islower(people[i][j]) == 0 && people[i][j] != ' ' && people[i][j] != '.')
{
if (people[i][j] == '\n')
{
conv_people[i][j] = '\0';
}
people[i][j] = conv_people[i][j] + 32;
}
}
}
//covert to numbers
char conv[42][102];
for (int i = 0; i < c; i++)
{
for (unsigned j = 0; j < strlen(people[i]); j++)
{
if (conv_people[i][j] == ' ' || conv_people[i][i] == '.' || conv_people[i][i] == '\n' || conv_people[i][i] == '\0')
{
conv[i][j] = '0';
}
if (conv_people[i][j] == 'a' || conv_people[i][j] == 'b' || conv_people[i][j] == 'c')
{
conv[i][j] = '2';
}
if (conv_people[i][j] == 'd' || conv_people[i][j] == 'e' || conv_people[i][j] == 'f')
{
conv[i][j] = '3';
}
if (conv_people[i][j] == 'g' || conv_people[i][j] == 'h' || conv_people[i][j] == 'i')
{
conv[i][j] = '4';
}
if (conv_people[i][j] == 'j' || conv_people[i][j] == 'k' || conv_people[i][j] == 'l')
{
conv[i][j] = '5';
}
if (conv_people[i][j] == 'm' || conv_people[i][j] == 'n' || conv_people[i][j] == 'o')
{
conv[i][j] = '6';
}
if (conv_people[i][j] == 'p' || conv_people[i][j] == 'q' || conv_people[i][j] == 'r' || conv_people[i][j] == 's')
{
conv[i][j] = '7';
}
if (conv_people[i][j] == 't' || conv_people[i][j] == 'u' || conv_people[i][j] == 'v')
{
conv[i][j] = '8';
}
if (conv_people[i][j] == 'w' || conv_people[i][j] == 'x' || conv_people[i][j] == 'y' || conv_people[i][j] == 'z')
{
conv[i][j] = '9';
}
}
}
//compare
int i = 0;
while (i < c)
{
if (strstr(conv[i], argv[1]) != NULL)
printf("%s, %s", people[i], numbers[i]);
if (strstr(numbers[i], argv[1]) != NULL)
printf("%s, %s", people[i], numbers[i]);
i++;
}
return 0;
}
The program takes a list of people and their phone numbers and searches it using argv[1]
The output always omits the first capital letter in each word
So if the file contains a name like: Barrack Obama
the program returns arrack bama
The numbers and converted names are working fine
I didn't want to post the whole thing because it's extremely ugly.
I've run the code and John is output as Éohn. It likely comes from
people[i][j] = conv_people[i][j] + 32;
because you never set any values in conv_people[i] except a terminator.
If I add this first line in the loop
strcpy(conv_people[i], people[i]);
then is outputs
john
with a lower case initial letter.
Aside: it is safer and convenient to use
people[i][j] = tolower(conv_people[i][j]);
which doesn't even need to be tested to see if an uppercase letter was passed.
I have an assignment that requires me to decode a Morse code string. The assignment specifies that:
- There will be no space between a character (example between . and -)
- Each character is separated by 1 space
- Each word is separated by 4 space
So far I have:
//Function to convert Morse code to text
char morsecode_to_text(char input[], int index){
int index_1 = 0, index_2 = 0, x = 0;
char output[100] = {'\0'};//Variable to keep output and later used to display text
char string[50] = {'\0'};//Variable to combine output
char *characters[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M","N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
char *morsecode[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---", "-.-",".-..","--","-.","---",".--.","--.-",
".-.","...","-","..-", "...-",".--","-..-","-.--","--..", ".----","..---","...--","....-", ".....", "-....",
"--...","---..","----.","-----"};
while(input[index] < 1000){
if(input[index] != '\0'){
if(input[index] == '.' || input[index] == '-') {
//Compare if input Morse code is similar to Morse code in array
while(input[index] == '.' || input[index] == '-'){
string[index_1] = input[index];
index += 1;
++index_1;
}//end of while loop
for(index_2=0;index_2<36;index_2++){
if(strcmp(string, morsecode[index_2]) == 0){
strcat(output, characters[index_2]);
x = 1;}
}//end of for loop
memset(string,0,sizeof(string));
//Check for spaces
}else if(input[index] == ' '){
if(input[index + 1] == ' ') {
if((input[index + 2] == ' ') && (input[index + 3] == ' ')){
strcat(output," ");
index += 4;
}else{
strcpy(output, "FAIL - WRONG MORSE CODE");
break;}
}else
index++;
}else if(input[index] == '\0' || input[index] == '\n'|| input[index] != '.' || input[index] != '-'){
break;}
}else
break;
}//end of while loop
if (x == 1){
printf("MORSE CODE TRANSLATED : SUCCESS - %s", output);
}else{
strcpy(output,"FAIL - WRONG MORSE CODE");
printf("%s", output);
}
}//end of Function to convert Morse code to text
void main()
{
char input[1000] = {'\0'}; //User input
char choice = 'y'; //Amount of time user wants to replay
int index = 0;
do{
system("cls");
printf("MORSE CODE TRANSLATOR PROGRAM\n");
printf("\nPlease enter a Morse code string: ");
fgets(input,1000,stdin);
morsecode_to_text(input, index);
printf("\n\nDo you want to try again? (Press 'y' for Yes and 'n' for No): ");
scanf("%c", &choice);
getchar();
}while(choice == 'y');//end of while loop
}//end of void main
And my output was supposed to look like this:
Example of Morse Code Translator Program execution
But the space function does not work correctly and therefore it displays this: Output of code
Thank you in advance!
a very simple one using your lookup tables
one non . or '-' - separates chars
more than one space - words.
const char *getLetter(const char *morse, int *ch)
{
char coded[10];
const char *startpos = morse;
while(*morse && *morse!= '.' && *morse != '-')
morse++;
if(!*morse) return NULL;
startpos = morse;
while(*morse && (*morse== '.' || *morse == '-'))
morse++;
if ((startpos - morse) > 9) return NULL;
strncpy(coded, startpos, morse - startpos);
coded[morse - startpos] = 0;
*ch = -1;
for(int index = 0; index < sizeof(morsecode) / sizeof(morsecode[0]); index++)
{
if(!strcmp(morsecode[index], coded))
{
*ch = characters[index][0];
break;
}
}
return morse;
}
int IsSpace(const char *morse)
{
return *morse && *morse ==' ' && *(morse + 1) == ' ';
}
int main(void)
{
char *morsestring = ".- -... -.-. -.. .- -... -.-. -.. ----------";
int ch = 0;
const char *nextpos = morsestring;
while((nextpos = getLetter(nextpos, &ch)) != NULL)
{
if(ch == -1)
{
printf(" *ERR* ");
}
else
{
printf("%c%s", ch, IsSpace(nextpos) ? " " : "");
}
}
printf("\n");
}
#include<stdio.h>
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
//See Easy instructions and have Fun of the Program
using namespace std;
void convert(char s[]);
void clrscr()
{
system("#cls||clear");
}
int main()
{
clrscr();
char string[100];
cout<<"INSTRUCTIONS : \n\n";
cout<<"1. Enter leaving single SPACE (like) : To enter MAN -> -- .- -. ";
cout<<endl<<"2. To Give SPACE, Enter s (small s) with Space (like) : To Enter MR A -> -- .-. s .-";
cout<<endl<<endl<<"SIMPLE !";
getch();
cout<<endl;
cout<<"Enter the Code to Convert into STRING : \n";
gets(string);
cout<<endl<<endl;
cout<<"Converting ....";
getch();
clrscr();
//for(int i =0 ; i < strlen(string) ; i++)
convert(string);
getch();
return 0;
}
void convert(char s[])
{
int j=0;
int i=0;
int c=0;
int add=0;
char a[10];
char f[100];
while(s[j] != '\0')
{
c=0;
while (c != 5)
{
a[c]='\0';
c++;
}
i=0;
while( s[j] != ' ' && s[j] != '\0')
{
a[i] = s[j];
j++;
i++;
}
if(s[j]== ' ' ){j++;}
a[i+1]='\0';
if(strcmp(a,".-") == 0)
{
f[add]='A';
add++;
f[add]='\0';
}
else if(strcmp(a,"-...") == 0){f[add]= 'B'; add++; f[add]='\0';}
else if(strcmp(a,"-.-.") == 0){f[add]= 'C'; add++; f[add]='\0';}
else if(strcmp(a,"-..") == 0){f[add]= 'D'; add++; f[add]='\0';}
else if(strcmp(a,".") == 0){f[add]= 'E'; add++; f[add]='\0';}
else if(strcmp(a,"..-.") == 0){f[add]= 'F'; add++; f[add]='\0';}
else if(strcmp(a,"--.") == 0){f[add]= 'G'; add++; f[add]='\0';}
else if(strcmp(a,"....") == 0){f[add]= 'H'; add++; f[add]='\0';}
else if(strcmp(a,"..") == 0){f[add]= 'I'; add++; f[add]='\0';}
else if(strcmp(a,".---") == 0){f[add]= 'J'; add++; f[add]='\0';}
else if(strcmp(a,"-.-") == 0){f[add]= 'K'; add++; f[add]='\0';}
else if(strcmp(a,".-..") == 0){f[add]= 'L'; add++; f[add]='\0';}
else if(strcmp(a,"--") == 0){f[add]= 'M'; add++; f[add]='\0';}
else if(strcmp(a,"-.") == 0){f[add]= 'N'; add++; f[add]='\0';}
else if(strcmp(a,"---") == 0){f[add]= 'O'; add++; f[add]='\0';}
else if(strcmp(a,".--.") == 0){f[add]= 'P'; add++; f[add]='\0';}
else if(strcmp(a,"--.-") == 0){f[add]= 'Q'; add++; f[add]='\0';}
else if(strcmp(a,".-.") == 0){f[add]= 'R'; add++; f[add]='\0';}
else if(strcmp(a,"...") == 0){f[add]= 'S'; add++; f[add]='\0';}
else if(strcmp(a,"-") == 0){f[add]= 'T'; add++; f[add]='\0';}
else if(strcmp(a,"..-") == 0){f[add]= 'U'; add++; f[add]='\0';}
else if(strcmp(a,"...-") == 0){f[add]= 'V'; add++; f[add]='\0';}
else if(strcmp(a,".--") == 0){f[add]= 'W'; add++; f[add]='\0';}
else if(strcmp(a,"-..-") == 0){f[add]= 'X'; add++; f[add]='\0';}
else if(strcmp(a,"-.--") == 0){f[add]= 'Y'; add++; f[add]='\0';}
else if(strcmp(a,"--..") == 0){f[add]= 'Z'; add++; f[add]='\0';}
else if(strcmp(a,"s") == 0 ){f[add]=' '; add++; f[add]='\0';}
}
cout<<f;
}
I am new to C. I am experienced in GWBASIC. In an effort to learn, I am attempting to write a program that will convert the individual chars in a string to a numerical value as so:
1 2 3 4 5 6 7 8 9
a b c d e f g h i
j k l m n o p q r
s t u v w x y z
For example, user input for string A could be 'dog',
said program would then store [d][o][g] as [4][6][7] in string B.
The below code works for a string w/up to four chars, but there must be a more efficient way of doing this.
int main()
{
char a[0];
char b[0];
scanf("%s",a);
if (a[0] == 'a' || a[0] == 'j' || a[0] == 's') b[0] = '1';
if (a[0] == 'b' || a[0] == 'k' || a[0] == 't') b[0] = '2';
if (a[0] == 'c' || a[0] == 'l' || a[0] == 'u') b[0] = '3';
if (a[0] == 'd' || a[0] == 'm' || a[0] == 'v') b[0] = '4';
if (a[0] == 'e' || a[0] == 'n' || a[0] == 'w') b[0] = '5';
if (a[0] == 'f' || a[0] == 'o' || a[0] == 'x') b[0] = '6';
if (a[0] == 'g' || a[0] == 'p' || a[0] == 'y') b[0] = '7';
if (a[0] == 'h' || a[0] == 'q' || a[0] == 'z') b[0] = '8';
if (a[0] == 'i' || a[0] == 'r') b[0] = '9';
if (a[1] == 'a' || a[1] == 'j' || a[1] == 's') b[1] = '1';
if (a[1] == 'b' || a[1] == 'k' || a[1] == 't') b[1] = '2';
if (a[1] == 'c' || a[1] == 'l' || a[1] == 'u') b[1] = '3';
if (a[1] == 'd' || a[1] == 'm' || a[1] == 'v') b[1] = '4';
if (a[1] == 'e' || a[1] == 'n' || a[1] == 'w') b[1] = '5';
if (a[1] == 'f' || a[1] == 'o' || a[1] == 'x') b[1] = '6';
if (a[1] == 'g' || a[1] == 'p' || a[1] == 'y') b[1] = '7';
if (a[1] == 'h' || a[1] == 'q' || a[1] == 'z') b[1] = '8';
if (a[1] == 'i' || a[1] == 'r') b[1] = '9';
if (a[2] == 'a' || a[2] == 'j' || a[2] == 's') b[2] = '1';
if (a[2] == 'b' || a[2] == 'k' || a[2] == 't') b[2] = '2';
if (a[2] == 'c' || a[2] == 'l' || a[2] == 'u') b[2] = '3';
if (a[2] == 'd' || a[2] == 'm' || a[2] == 'v') b[2] = '4';
if (a[2] == 'e' || a[2] == 'n' || a[2] == 'w') b[2] = '5';
if (a[2] == 'f' || a[2] == 'o' || a[2] == 'x') b[2] = '6';
if (a[2] == 'g' || a[2] == 'p' || a[2] == 'y') b[2] = '7';
if (a[2] == 'h' || a[2] == 'q' || a[2] == 'z') b[2] = '8';
if (a[2] == 'i' || a[2] == 'r') b[2] = '9';
if (a[3] == 'a' || a[3] == 'j' || a[3] == 's') b[3] = '1';
if (a[3] == 'b' || a[3] == 'k' || a[3] == 't') b[3] = '2';
if (a[3] == 'c' || a[3] == 'l' || a[3] == 'u') b[3] = '3';
if (a[3] == 'd' || a[3] == 'm' || a[3] == 'v') b[3] = '4';
if (a[3] == 'e' || a[3] == 'n' || a[3] == 'w') b[3] = '5';
if (a[3] == 'f' || a[3] == 'o' || a[3] == 'x') b[3] = '6';
if (a[3] == 'g' || a[3] == 'p' || a[3] == 'y') b[3] = '7';
if (a[3] == 'h' || a[3] == 'q' || a[3] == 'z') b[3] = '8';
if (a[3] == 'i' || a[3] == 'r') b[3] = '9';
printf("%s\n",b);
return 0;
}
Assuming that your compiler uses an ASCII encoding then you can use the following simple arithmetic to get your answer:
1 + (strA[i] - 'a') % 9
You really don't want to implement this with a long list of if statements or indeed a switch statement.
Naturally you will have input validation issues if you have non alphabetical characters, numeric characters, upper-case characters and so on. I presume you can simply ignore those for a learning exercise.
To correct your original approach, you need to do two things:
Use single quotes around character constants;
Use == to check for equality;
Terminate statements with ;.
... so your snippet becomes:
if (strA[0] == 'a')
strB[0] = '1';
if (strA[0] == 'b')
strB[0] = '2';
if (strA[0] == 'c')
strB[0] = '3';
You can type the following exactly into a gwbasic editor and it will solve your problem
10 INPUT A$
12 L = LEN(A$)
15 FOR T = 1 TO L
20 M$ = MID$(A$,T,1)
25 GOSUB 70
30 B$ = B$ + V$
35 NEXT T
40 PRINT B$
50 END
55 REM -----------------
70 REM - Subroutine to convert m$ into v$
72 X = ASC(M$) : REM this is the ascii value of m$ (eg. "a" = 97)
74 X = X - 96 : REM so that 97 becomes "1"
80 IF X > 9 THEN X = X - 9 : GOTO 80
90 V$ = STR$(X) : REM just converting to a string type variable
95 RETURN : REM takes you back to line 30 where this value is added to the
96 REM final resulting B$ - when I say added I mean a char added to a string
97 REM such that "APPL" + "E" = "APPLE"
98 REM ------------------------------------------ DONE
For ASCII, it would go somewhat like:
... make sure strB has enough space ...
for (i = 0; i < ... length of strA ... ; i++) {
/* you should always check your input is valid */
if (strA[i] >= 'a' && strB[i] <= 'z')
strB[i] = (strA[i] - 'a') % 9 + 1;
else
strB[i] = ... some default value ...
}
For EBCDIC:
for (i = 0; i < ... length of strA ... ; i++) {
/* you should always check your input is valid */
if (strA[i] >= 'a' && strB[i] <= 'r')
strB[i] = strA[i] & 0xF;
else if (strA[i] >= 's' && strB <= 'z')
strB[i] = (strA[i] & 0xF) - 1;
else
strB[i] = ... some default value ...
}
Have a closer look at an ASCII table. You'll see that all letters are encoded with some integer value. As a start, if you only have lower case letters, it's enough to substract the character code of 'a' from any letter to get what you want
int nr = strA[0] - 'a' + 1;
//now you'd need to convert back to a string; better: strB should be an array of integer
Also, = is the assignment operator; you need to use == to check for equality.
try this :)
int strB[MAX_LEN] = {0};
char *strA = malloc (MAX_LEN * sizeof(char));
int i,c = 0,x;
scanf("%s",strA);
for(i = 0 ; i<strlen(strA) ; i++){
x = strA[i] - 'a' + 1;
if(x >= 1 && x <= 9)
strB[c] = x;
else if(x <= 18){
strB[c] = x - 10;
else if(x <= 26){
strB[c] = x - 19;
if(x <= 26)
c++;
}
or you can use ninjalj approach in the for loop, if you aleady checked the input:
for(i=0 ; i<strlen(strA) ; i++){
strB[i] = (strA[i] - 'a') % 9 + 1;
}
or this :
for(i=0 ; i<strlen(strA) ; i++){
if(strA[i] >= 'a' && strA[i] <= 'z'){
strB[c] = (strA[i] - 'a') % 9 + 1;
c++;
}
}