Change place characters - C

Asked

Viewed 758 times

0

I need to write a C program that takes a word (string) as a parameter and generates a new string consisting of the original string with the following changes:

The first character of the string must be placed at the end of the string;

The suffix "ay" should be added at the end of this string. Consider that the string received by the program will have a maximum of 100 characters. No use no library function <string.h>

But I’m having a hard time switching places.

Code:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv){
    
    char string[101];
    char letra[2];
    char comp[3]="ay";
    int count, i;
    
    printf("String:  ");
    scanf("%s", string);
    
    for(count=1; string[i]!='\0'; i++){
        count++;
    }
}

1 answer

0


I believe this code solves the problem...

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv){

    char string[101];
    char letra[2];
    char comp[3]="ay";
    int count, i;

    printf("String:  ");
    scanf("%s", string);

    for(count=0; string!='\0'; i++){
        count++;
    }
    letra[0]=string[0];
    string[0]=string[count];
    string[count]=letra[0];
    string[count+1]='a';
    string[count+2]='y';
    printf("%s",string);
}

If I could use <string.h> it would be easier:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()

{

    char string[101], backup;

    int tamanho_str;

    printf("String:  ");
    scanf("%s", string);

    tamanho_str = strlen (string);

    backup = string[tamanho_str-1];

    string [tamanho_str-1] = string [0];

    string [0] = backup;

    strcat (string, "ay");

    printf ("\nstring final: %s\n", string);

    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.